【问题标题】:C++11 compiler error when using decltype(var) followed by internal type of "var"使用 decltype(var) 后跟“var”的内部类型时出现 C++11 编译器错误
【发布时间】:2012-12-29 03:22:05
【问题描述】:

我使用的是 Visual C++ 2010,这是我的代码 sn-p:

std::set<int> s;
decltype(s)::value_type param = 0;

我收到以下错误消息,有人可以帮助我吗?

> error C2039: 'value_type' : is not a member of '`global namespace''
> error C2146: syntax error : missing ';' before identifier 'param'

【问题讨论】:

  • 这是一个编译器错误

标签: c++ visual-studio-2010 c++11 compiler-errors decltype


【解决方案1】:

这是去年在 Connect 上提出的 Visual Studio 错误。是issue 757545 ("Cannot use decltype before scope operator")

该问题旁边列出了一个解决方法,它实际上与@iammillind 的相同,除了它使用了在 C++11 发布之前不久从&lt;functional&gt; 中删除的std::identity,无论出于何种原因。 (std::common_type 带有一个模板参数是等效的;std::remove_reference 在某些情况下是相同的。)

【讨论】:

  • @Potatoswatter:是的,应该这样做,尤其是在 Connect 上发布的解决方法意味着 VS 支持它。我只希望解决方法提交者记下它; surprise, surprise, MSDN doesn't care.
【解决方案2】:

我看到使用 g++ 4.7.2 版本,代码编译得很好。所以这可能是 MSVS 中的编译器错误
暂时可以试试下面的招数:

#ifdef COMPILER_BUG_STILL_THERE
template<typename T> struct Get { typedef T type; };
#define DECLTYPE(VAR) Get<decltype(VAR)>::type
#else
#define DECLTYPE(VAR) decltype(VAR)
#endif

将其用作:

DECLTYPE(s)::value_type param = 0;

免责声明:当然有了这个技巧,你可能不得不在模板中使用typename。为此,您可以再拥有 1 个宏,例如 #define TDECLTYPE(VAR) typename DECLTYPE(VAR)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    相关资源
    最近更新 更多