如何解决在MSVC 2012中使用K = ...`的问题?

Cla*_*diu 2 c++ templates types c++11 visual-studio-2012

似乎MSVC 2012不支持using K = ...;-type声明.例如,使用代码:

template <class Map>
inline void foo(Map &m)
{
  using K = typename Map::key_type;
  using V = typename Map::mapped_type;
  // ...
}
Run Code Online (Sandbox Code Playgroud)

结果是语法错误:

error C2143: syntax error : missing ';' before '='
error C2873: 'K' : symbol cannot be used in a using-declaration
Run Code Online (Sandbox Code Playgroud)

如何在不升级编译器的情况下解决MSVC 2012的这个缺失功能​​?

dlf*_*dlf 8

微软对C++ 11的支持是不完整的,这是VS2012中缺少的东西之一.但在这种情况下,你应该能够使用一个老式的typedef; 例如:

typedef typename Map::key_type K;

此变通办法崩溃的地方是模板类型:

template<typename T>
using Bar = Foo<T>; // ok if your compiler supports it

template<typename T>
typedef Foo<T> Bar; // doesn't compile
Run Code Online (Sandbox Code Playgroud)

但是你仍然至少有这个选择:

template<typename T>
struct Bar
{
   typedef Foo<T> type;
};
Run Code Online (Sandbox Code Playgroud)