2

I have this code which I think should work, but it is not!

 class base
 {
     std::array<uint8_t, 8> m_ID;
 public:
     base(std::array<uint8_t, 8> id) :m_ID(id)
     {

     }
 }
 template<char ...Ts>
 class derived:base(Ts...)
 {
 }
 class MyClass: public derived<'1','2','3','4','5','6','7','8'>
 {
 }

How can I do this? The idea is that I can pass the ID value from template values.

I am getting error that MyClass is not complete. (Incomplete type is not allowed)

1
  • I find it odd that base has an array of 8 values which are populated from a parameter pack. What if the user provides fewer/more than 8 parameters? Commented Dec 12, 2017 at 15:57

1 Answer 1

3

You just have to call the base class constructor properly:

#include <array>
#include <cstdint>

 class base
 {
     std::array<std::uint8_t, 8> m_ID;
 public:
     base(std::array<std::uint8_t, 8> id) :m_ID(id)
     {

     }
 };

 template<char ...Ts>
 class derived: public base
 {
    public:
    derived() : base{ { Ts... } } { }
 };

class MyClass: public derived<'1','2','3','4','5','6','7','8'>
 {
 };

 int main() {
    MyClass d;
 }

Note that in the constructor initializer list, the inner pair of braces is needed to convert the single uint8_ts to an array.

Sign up to request clarification or add additional context in comments.

2 Comments

You forgot semicolon after the MyClass class definition. And (void) MyClass d; is not a valid C++ line of code.
@Constructor Feel free to improve answers on SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.