Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template aliases don't work

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?

like image 966
Dani Avatar asked May 22 '26 18:05

Dani


1 Answers

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
}
like image 86
Howard Hinnant Avatar answered May 25 '26 06:05

Howard Hinnant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!