In the comments of the accepted answer of C++11: How to alias a function?, Johannes Schaub - litb said that perfect forwarding with explicit template arguments might work with code like this:
template<typename T1, typename T2>
void bar(int i, float f)
{
std::cout << i << ", " << f << std::endl;
std::cout << sizeof(T1) << ", " << sizeof(T2) << std::endl;
}
template<typename T1, typename T2>
void bar(long i)
{
std::cout << i << std::endl;
std::cout << sizeof(T1) << ", " << sizeof(T2) << std::endl;
}
template<typename ...ExplicitArgs, typename ...Args>
void foo(Args&&... args)
{
bar<ExplicitArgs...>(std::forward<Args>(args)...);
}
int main()
{
foo<char, char[2]>(123, 12.5);
foo<char[3], char[4]>(426);
return 0;
}
I have tested this with g++ and clang and it compiled correctly with both compilers and produced the expected output:
123, 12.5
1, 2
426
3, 4
Does the standard define this to be correct?
My other question is if this can be made to work with multiple kinds of template parameters at once (say if bar took a type and an int as template parameters).
Aucun commentaire:
Enregistrer un commentaire