c++ - Visual Studio 2010 的模板问题

标签 c++ visual-studio-2010 templates

使用 Linux 中的 GCC,以下代码可以很好地编译。但是使用 visual studio 时,会出现错误 C2893。你有什么想法 ?

struct A
{
};
struct B
{
    typedef A Data;
};
struct C
{
    typedef B Data;
};
template<typename Type>
typename Type::Data::Data  test( Type in)
{
    return Type::Data::Data();
}

int main(){
    C c;
    C::Data::Data a;//works
    test(c);//error C2893: The specialization of the function template 'Type :: Data :: {ctor} test (Type)' failed
}

非常感谢

Avakar 的解决方案:因为 Data::Data 指的是 Data 类型的构造函数,所以你必须使用这个习惯用法:

typename identity<typename Type::Data>::type::Data

最佳答案

编译器无法专门化函数模板。导致此错误的原因可能有很多。通常,解决 C2893 错误的方法是检查函数的签名并确保您可以实例化每种类型。

关于c++ - Visual Studio 2010 的模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542232/

相关文章:

ios - Azure 通知中心 Xamarin 为多个模板注册多个

c++ - 如何反转 hana::string

c++ - 如何将 char 添加到 istringstream?

c++ - 在 main 之前立即调用一个函数

c++ - 编写并执行创建无限数量进程的 C 或 C++ 程序。

c++ - cmake:构建一个程序的多个版本

c++ - Visual Studio C 性能

python - 在 python 的 visual studio 工具中按下 ctrl+F5 后,控制台窗口立即关闭

visual-studio-2010 - 在 Visual Studio 2012 中编译 Moles 时出现错误 "The type or namespace xxxx does not exist"

c++ - 如何结合就地转换和复制转换?