Why is the integer parameter package used, forbidden after the type parameter package in C ++ 11?

The question is almost meaningless without an example. So here is what I am trying to do.

In general, C ++ allows the following:

template<class T, class U, T t, U u> void func() {} func<char, int, 'A', 10>(); 

But it seems that its natural variational extension does not work.

 template<class...T, T... t> void func() {} func<char, int, 'A', 10>(); 

Both clang and g ++ 4.7 reject the above code. An error is displayed where the instance is being created. It seems to me that two variational lists should be analyzed unambiguously, because the first has types, and the other has only integral values.

If the above is not intended to work, I think the following will not work.

 template <class Ret, class... Args, Ret (*func)(Args...)> class Foo {}; 

I think the Foo pattern is a pretty useful thing.

+8
c ++ c ++ 11 variadic-templates
source share
2 answers

( Optional: By directly answering your first question, you can also turn template<class...T, T... t> void func() {} into a template-inside-template. This does not work in g + + 4.6, but in clang 3.0, so it took me a while to find it.)

Place the template inside the template:

 template<class ... T> struct func_types { template <T ... t> static void func_values() { // This next line is just a demonstration, and // would need to be changed for other types: printf("%c %d\n", t...); } }; int main() { func_types<char, int> :: func_values<'A', 10>(); } 

Is the template inside inside the template acceptable? An alternative is to use tuples with func< tuple<char,int> , make_tuple('A',10) > . I think this is doable, but you may have to flip your own tuple (it seems that make_tuple is not constexpr .

Finally, you can implement your Foo template as follows:

 template<typename Ret, typename ...Args> struct Foo { template< Ret (*func)(Args...)> struct Bar { template<typename T...> Bar(T&&... args) { cout << "executing the function gives: " << func(std::forward(args)...) << endl; } }; }; int main () { Foo<size_t, const char*> :: Bar<strlen> test1("hi"); Foo<int, const char*, const char*> :: Bar<strcmp> test2("compare","these"); } 

This last code is on ideone . To demonstrate, I implemented a constructor to redirect the arguments to a function that contains the code in the template.

+8
source share

Because no one thought it was worth having this feature. The design of the variation template was to be simple and working. Other potentially advanced and useful features included as well.

+2
source share

All Articles