October 22, 2024
Chicago 12, Melborne City, USA
templates

Issue calling a variadic template constructor


The following code doesn’t work, it fails because it cannot find a constructor with a <int, bool> signature.

I know can’t specify explicitely the template parameters in the case of a constructor.

Why does it fail template parameter deduction? Is there any way to make it work? I know I can use a template factory instead of a constructor as a workaround (Which I will likely do, but I’m curious if it’s doable with constructors only)

(Requires c++11 for delegating constructors)

template<size_t S>
struct Foo
{
    Foo() {}
    
    template<typename ... FirstArgsT, typename LastArgT>
    Foo(const FirstArgsT& ... FirstArgs, const LastArgT& LastArg) : Foo(FirstArgs...)
    {
        static_assert(sizeof...(FirstArgsT)+1<=S);
        TypeSizes[sizeof...(FirstArgsT)] = sizeof(LastArgT);
    }
        
    size_t TypeSizes[S];
};


int main()
{
    int arg1 = 3;
    bool arg2 = false;
    Foo<2> foo(arg1, arg2);  //error: no matching function for call to ‘Foo<2>::Foo(int&, bool&)’
    return 0;
}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video