开发者

Compilation Error on Recursive Variadic Template Function

I've prepared a simple variadic template test in Code::Blocks, but I'm getting 开发者_开发知识库an error:

No matching function for call to 'OutputSizes()'

Here's my source code:

#include <iostream>
#include <typeinfo>

using namespace std;

template <typename FirstDatatype, typename... DatatypeList>
void OutputSizes()
{
    std::cout << typeid(FirstDatatype).name() << ": " << sizeof(FirstDatatype) << std::endl;
    OutputSizes<DatatypeList...>();
}

int main()
{
    OutputSizes<char, int, long int>();
    return 0;
}

I'm using GNU GCC with -std=C++0x. Using -std=gnu++0x makes no difference.


Here's how you disambiguate the base case:

#include <iostream>
#include <typeinfo>

template <typename FirstDatatype>
void OutputSizes()
{
    std::cout << typeid(FirstDatatype).name() << ": " << sizeof(FirstDatatype) << std::endl;
}

template <typename FirstDatatype, typename SecondDatatype, typename... DatatypeList>
void OutputSizes()
{
    OutputSizes<FirstDatatype>()
    OutputSizes<SecondDatatype, DatatypeList...>();
}

int main()
{
    OutputSizes<char, int, long int>();
}


It's because you didn't provide a base case. You extracted the last data type of the variadic template parameter- then you tried to match an empty variadic parameter to a function taking a type and a variadic parameter. You need to provide a "base case" for when the variadic parameter is empty.

using namespace std;

template <typename FirstDatatype, typename... DatatypeList>
void OutputSizes()
{
    std::cout << typeid(FirstDatatype).name() << ": " << sizeof(FirstDatatype) << std::endl;
    OutputSizes<DatatypeList...>();
}

template<typename... DataTypeList>
void OutputSizes() // We're called when there's no template arguments
                   // left in the pack
{
}

int main()
{
    OutputSizes<char, int, long int>();
    return 0;
}

Edit: The many-zero overloads I have shown here actually only work when you take actual run-time arguments too based on the template types. If you only take template parameters directly, you have use two-one recursion like shown in Howard Hinnant's answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜