成员函数模板,参数数量取决于整体模板参数 [英] Member function template with the number of parameters depending on an integral template parameter
问题描述
我有以下类模板:
template<class T, unsigned N>
class MyClass;
其中 T
是某种类型, N
- 组件数。可以使用 MyClass {a1,a2,a3}
初始化类,其中参数数等于 N
。
where T
is some type, N
- number of components. It is possible to initialize the class using MyClass{a1, a2, a3}
where the number of arguments is equal to N
.
我想添加一个成员函数模板(让我们将它命名为
将满足以下要求: foo
> MyClass
I want to add a member function template (let's name it foo
) of MyClass
that would meet the following requirements:
- 它由另一个类型
T2
(即template< class T2> void foo(..)
) - 构造
MyClass< T,N>
,但不能少,不能多。违反此操作会导致编译时错误。 - 从参数类型中推导出
T2
。也就是说我想要可以调用foo({a1,a2,a3})
或foo(a1,a2,a3)
或类似的,不必每次键入< double>
或MyClass< double,N>
/ li>
- It is templated by another type
T2
(i.e.template<class T2> void foo(..)
) - It accepts enough data to construct
MyClass<T,N>
, but not less and not more. Violating this results in a compile-time error. - It deduces
T2
from the types of the parameters. I.e. I want that it would be possible to callfoo({a1, a2, a3})
orfoo(a1, a2, a3)
or similar, without typing<double>
orMyClass<double,N>
every time.
有没有办法实现该函数,以满足上述要求?
Is there a way to implement the function so that the above requirements are satisfied?
我已经考虑过和/或试过以下解决方案:
I've already thought about and/or tried the following solutions:
1)显而易见的:
...
template<class T2>
void foo(MyClass<T2, N> arg);
...
a.foo({1,2,3}); //compile-time error
原则上不能工作,因为支持的初始化列表是非推导的上下文,因此他们不能推断任何类型。这是非常不幸的,如果这个工作,我会很高兴。
Can't work in principle, because braced initializer lists are a non-deduced context, thus they can't deduce any types. That's quite unfortunate, I'd be very happy if this worked.
2) initializer_list
原则上不能工作,因为它不能在编译时检查参数的数量。
Can't work in principle, because it can't check the number of arguments at compile-time.
3) / strong>
3) Variadic template magic
类似下面的函数将是整洁的:
Something like the function below would be neat:
template<class...T2, class std::enable_if<sizeof...(T2) == N, int>::type = 0>
void foo(T2... args);
..
foo(1,2,3);
但是,我无法让它工作 - T2仍然无法推断。也许有人知道为什么?我使用了GCC4.7 20120121快照。
However, I couldn't get it to work - T2 still couldn't be deduced. Maybe someone knows why? I used GCC4.7 20120121 snapshot.
4)丑陋的
基本上这和上面的一样,只是扩展到不同N的多个重载。我最好重新实现 MyClass
作为一组专用化不同的
Essentially this is the same as the above one, just expanded into several overloads for different N. I would better reimplement MyClass
as a set of specializations for different N
s than to use this one.
template<class T2, class std::enable_if<N == 1, int>::type = 0>
void fun(T2 a1); //if N == 1
template<class T2, ..>
void fun(T2 a1, T2 a2); //if N == 2
template<class T2, ..>
void fun(T2 a1, T2 a2, T2 a3); //if N == 3
...
推荐答案
您的第三个变体的第二个非类型参数应该有 typename
不类
:
Your third variant's second non-type param should have prefix typename
not class
:
template<class...T2, typename std::enable_if<sizeof...(T2) == N, int>::type = 0>
void foo(T2... args);
..
foo(1,2,3);
Gcc 4.7.0快照与模板有一些错误我想,如果你尝试它与gcc 4.6.2 / 1它将工作。
Gcc 4.7.0 snapshots has some bugs with templates I guess, if you try it with gcc 4.6.2/1 it shall work.
这篇关于成员函数模板,参数数量取决于整体模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!