递归变量模板函数的编译错误 [英] Compilation Error on Recursive Variadic Template Function

查看:177
本文介绍了递归变量模板函数的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Code :: Blocks中准备了一个简单的可变参数模板测试,但我收到一个错误:

I've prepared a simple variadic template test in Code::Blocks, but I'm getting an error:


函数调用'OutputSizes()'

No matching function for call to 'OutputSizes()'

这是我的源代码:

#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;
}



我使用GNU GCC with -std = C ++ 0x 。使用 -std = gnu ++ 0x 没有区别。

推荐答案

以下是如何消除基本案例的歧义:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆