Question

I'm trying to get template aliases to work on clang but it doesn't work although the reference sheet says it does

~~~~>$ cat template_alias.cpp 
#include <vector>

using namespace std;

template<typename T>
using DoubleVec = vector<vector<T>>;

int main() { return 0; }

~~~~>$ clang template_alias.cpp -o template_alias

template_alias.cpp:6:19: warning: alias declarations accepted as a C++0x extension [-Wc++0x-extensions]
using DoubleVec = vector<vector<T>>;
                  ^
template_alias.cpp:6:34: error: a space is required between consecutive right angle brackets (use '> >')
using DoubleVec = vector<vector<T>>;
                                 ^~
                                 > >
template_alias.cpp:6:1: error: cannot template a using declaration
using DoubleVec = vector<vector<T>>;
^
1 warning and 2 errors generated.

~~~~>$ clang -std=c++0x template_alias.cpp -o template_alias

template_alias.cpp:6:1: error: cannot template a using declaration
using DoubleVec = vector<vector<T>>;
^
1 error generated.

Am I doing it wrong?

Was it helpful?

Solution

Your second command (with -std=c++0x) is correct, as is your test case. You might be using a version of clang prior to its support for template aliases. You can check for this by doing:

#if __has_feature(cxx_alias_templates)

Here is a complete list of feature-test macros that clang uses:

http://clang.llvm.org/docs/LanguageExtensions.html#checking_upcoming_features

Here is one, somewhat unpleasant, way to deal with the transition period between support of template aliases and not:

#include <vector>

using namespace std;

#if __has_feature(cxx_alias_templates)

template<typename T>
using DoubleVec = vector<vector<T>>;

#else

template<typename T>
struct DoubleVec {
    typedef vector<vector<T> > type;
};

#endif

int main()
{
#if __has_feature(cxx_alias_templates)
    DoubleVec<int> v;
#else
    DoubleVec<int>::type v;
#endif
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top