【问题标题】:typedef and using in c++11typedef 并在 c++11 中使用
【发布时间】:2015-04-20 06:42:01
【问题描述】:
#include <iostream>

struct cls {
    using type = double;          //case 1
    // typedef double type;         //case 2
};

template<typename T>
void foo(typename T::type) {
    std::cout<<"T::type\n";
}

int main() {
    foo<cls>(22.2);
}

我相信using 并被用来代替typedef。 在上面的代码中,case 1 出现错误,case 2 却没有。

error: expected nested-name-specifier before 'type'

有人能解释一下为什么吗?

【问题讨论】:

标签: templates c++11 typedef


【解决方案1】:

您的编译器doesn't support C++11 type aliases 对它们的支持有限(MSVC 在不使用时会错误地解析模板类型)或者没有激活 C++11 选项,请确保您使用的是 C++11兼容(并启用)一个,即升级您的编译器。

struct cls {
    using type = double;         // Doesn't work on pre-C++11
    // typedef double type;      // Works on pre-C++11
};

我还建议阅读 C++11 中的 difference between using and typedef

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    相关资源
    最近更新 更多