gpt4 book ai didi

c++ - VC++ Variadic-Template-Template 错误 C2244 : unable to match function definition to an existing declaration

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:58:46 27 4
gpt4 key购买 nike

这可能是新手的错误,我正在使用 Microsoft Visual C++ 编译器 2012 年 11 月 CTP,当我尝试编译它时:

#include <tuple>

class A
{
public:
template <class... Fs, template <class...> class T>
void foo(T<Fs...>);
};

template <class... Fs, template <class...> class T>
void A::foo(T<Fs...>)
{
}

int main()
{
A a;
a.foo(std::make_tuple(10,10));
}

我收到以下错误:

error C2244: 'A::foo' : unable to match function definition to an existing declaration
definition
'void A::foo(T<Fs...>)'
existing declarations
'void A::foo(T<Fs...>)'

如果我内联它编译的函数并做我想做的事,但我必须将函数放在头文件中

但是下面的代码编译没有错误:

#include <vector>

class A
{
public:
template <class Fs, template <class> class T>
void foo(T<Fs>);
};

template <class Fs, template <class> class T>
void A::foo(T<Fs>)
{
}

int main()
{
A a;
a.foo(std::vector<int>());
}

所以问题是我怎样才能用可变参数模板实现同样的事情

编辑:

按照建议在 Microsoft Connect 上提交错误报告 ID :771567

vector 示例也很糟糕(它会给出 <std::_Simple_types<int>,std::_Vector_val> ,但它确实可以编译),但是如果你尝试使用一对和模板参数:

template <class F, class G, template <class,class> class T>

你会得到<F,G,std::pair>这就是我的意思

最佳答案

From MSDN regarding Compiler Error C2244 :

You cannot partially specialize a function template.

我还认为你必须在类前面声明模板:

template<typename T>
class MyClass
{
};

关于c++ - VC++ Variadic-Template-Template 错误 C2244 : unable to match function definition to an existing declaration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433196/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com