C++11 compiler error when using decltype(var) followed by internal type of “var”

后端 未结 2 890
青春惊慌失措
青春惊慌失措 2021-01-17 22:47

I\'m using Visual C++ 2010, and here\'s my code snippet:

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

I got the following erro

2条回答
  •  深忆病人
    2021-01-17 23:24

    I see that with g++ 4.7.2 version, the code compiles fine. So it could be a compiler bug in MSVS.
    For time being you can try below trick:

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

    Use it as:

    DECLTYPE(s)::value_type param = 0;
    

    Disclaimer: Ofcourse with this trick, you may have to use typename when inside templates. For that you can have 1 more macro such as #define TDECLTYPE(VAR) typename DECLTYPE(VAR)

提交回复
热议问题