【问题标题】:Implementing a Type Defined in a Template Argument [duplicate]实现模板参数中定义的类型
【发布时间】:2015-07-20 11:02:30
【问题描述】:

此代码在 Visual Studio 2013 上运行,但不在 gcc 4.9.2 上:

template <typename T>
void foo(){
    T::value_type bar('a');

    cout << bar << endl;
}

int main() {
    foo<string>();

    return 0;
}

似乎 Visual Studio 2013 推断类型以允许我实现模板参数中定义的类型,而 gcc 4.9.2 does not

是否有一种解决方法可以在 gcc 中允许这种行为?

【问题讨论】:

  • 您遇到的错误是什么,这会有所帮助

标签: c++ visual-studio templates gcc types


【解决方案1】:

您需要告诉编译器T::value_type 命名了一个类型,以便它知道如何解析声明。

typename T::value_type bar('a');

请参阅this question,了解有关您需要在何处以及为何需要这样做的更多详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2022-01-14
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多