Member function template with the number of parameters depending on an integral template parameter

前端 未结 4 494
闹比i
闹比i 2021-02-04 10:27

I have the following class template:

template
class MyClass;

where T is some type, N - num

4条回答
  •  失恋的感觉
    2021-02-04 10:55

    Maybe like this:

    #include 
    #include 
    
    template 
    class MyClass
    {
        template 
        typename std::enable_if, Args...>::value>::type
        foo(Args &&... args)
        {
            // for example:
            MyClass m(std::forward(args)...);
            // ...
        }
    };
    

    This will work only if MyClass has a constructor that accepts the relevant arguments directly (like MyClass(A1, A2, A3)), but I don't think it works if MyClass has a constructor that requires an initializer list, nor will it work if MyClass is an aggregate that requires brace-initialization.

    That said, it doesn't look like your MyClass could possibly accept an initializer list, since you said that it has to take precisely N arguments, which an IL cannot promise.

提交回复
热议问题