English 中文(简体)
履历表
原标题:Compilation Error on Recursive Variadic Template Function

I ve在《刑法》中准备了一个简单的理论模板测试:24小时,但Im正出现错误:

(a) 没有要求产出的配对功能

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 采用海合会-std=C++0x。 采用<代码>-std=gnu++0x,无任何区别。

最佳回答

这里,你如何质疑基例:

#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>();
}
问题回答

这是因为你没有提供基例。 您提取了最后一类的单程模板参数,然后试图将空洞的参数与一种类型和单程参数的功能相匹配。 你们需要提供“基准案例”,以证明单程参数是空的。

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 我在这里实际上只是在你根据模板类型得出实际操作时间论点时才表明工作。 如果你只直接采用模板参数,那么你就采用了霍华德·印地安的答复中所示的两种再次入侵。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签