Переменное число параметров в шаблоне функции
От: _NN_ www.nemerleweb.com
Дата: 06.08.13 19:53
Оценка: 37 (2)
Почему следующий код не компилируется ?
Разве нельзя создать переменное количество аргументов в функции шаблоне с переменным количеством типов ?

template<typename... T>
struct caller
{
    template<T... F>
    static void f()
    {
    }
};

int main()
{
    caller<int>::f<1>();
}


+ g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp

main.cpp: In function ‘int main()’:
main.cpp:13:13: error: no matching function for call to ‘caller<int>::f()’
c::f<1>();
^

main.cpp:13:13: note: candidate is:
main.cpp:5:17: note: template<T ...F> static void caller<T>::f() [with T ...F = {F ...}; T = {int}]
static void f()
^

main.cpp:5:17: note: template argument deduction/substitution failed:
main.cpp:13:13: note: invalid template non-type parameter

c::f<1>();


А если передавать через аргументы то работает:
template<typename... T>
struct caller
{
    static void f(T... F)
    {
    }
};
 
int main()
{
    caller<int>::f(1);
}


И через класс тоже:
struct caller
{
    template<T... F>
    struct inner
    {
        static void f()
        {
        }
    };
};
 
int main()
{
    caller<int>::inner<1>::f();
}
http://rsdn.nemerleweb.com
http://nemerleweb.com
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.