3

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?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Daniel
  • 30,896
  • 18
  • 85
  • 139

1 Answers1

7

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
}
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • It is normal for what ships in a commercial project to lag what is on tip-of-trunk of an open source project. – Howard Hinnant Oct 09 '11 at 15:46
  • Do you know of any way to update the clang in xCode? will it still be able to compile to iphone and be approved by app store? – Daniel Oct 09 '11 at 15:49