template-aliases

template aliases and sfinae

瘦欲@ 提交于 2019-12-01 15:22:00
问题 In the case of a substitution failure involving a template alias ( e.g. a template alias on a missing member typename, as in the code snippet below), should an error be triggered ? Clang and gcc seem to disagree on this: // some types struct bar { }; struct foo { typedef void member_type; }; // template alias template<class T> using member = typename T::member_type; template<class T> void baz(... ) { } // only works for gcc, clang fails with: no type named 'member_type' // in 'bar' template

Why does alias template give a conflicting declaration?

我只是一个虾纸丫 提交于 2019-12-01 02:03:17
The port of some C++11 code from Clang to g++ template<class T> using value_t = typename T::value_type; template<class> struct S { using value_type = int; static value_type const C = 0; }; template<class T> value_t<S<T>> // gcc error, typename S<T>::value_type does work const S<T>::C; int main() { static_assert(S<int>::C == 0, ""); } gives different behavior for Clang (versions 3.1 through SVN trunk) versus for any g++ version. For the latter I get errors like this prog.cc:13:13: error: conflicting declaration 'value_t<S<T> > S< <template-parameter-1-1> >::C' const S<T>::C; ^ prog.cc:8:29:

Why does alias template give a conflicting declaration?

别来无恙 提交于 2019-11-30 21:22:47
问题 The port of some C++11 code from Clang to g++ template<class T> using value_t = typename T::value_type; template<class> struct S { using value_type = int; static value_type const C = 0; }; template<class T> value_t<S<T>> // gcc error, typename S<T>::value_type does work const S<T>::C; int main() { static_assert(S<int>::C == 0, ""); } gives different behavior for Clang (versions 3.1 through SVN trunk) versus for any g++ version. For the latter I get errors like this prog.cc:13:13: error:

Alias template, partial specialization and the invalid parameter type void

此生再无相见时 提交于 2019-11-30 18:31:54
Consider the following code: template<typename F> struct S; template<typename Ret, typename... Args> struct S<Ret(Args...)> { }; template<typename... Args> using Alias = S<void(Args...)>; int main() { S<void(int)> s; Alias<int> alias; } It works fine, as expected and both the line involving S and the one involving Alias define under the hood the same type S<void(int)> . Now, consider the following changes: int main() { S<void(void)> s; // this line compiles Alias<void> alias; // this line does not } I expected it to compile, for reasons that are similar to the ones above mentioned. It goes

Pack expansion for alias template

让人想犯罪 __ 提交于 2019-11-30 10:55:33
问题 It seems that a pack argument can be expanded only in the place of a pack parameter of an alias template. This is not true for a class or a function template: template <class T, class... Args> struct x { using type = T; }; template <class T, class... Args> using x_t = typename x<T, Args...>::type; template <class... Args> using x_fix_t = typename x<Args...>::type; template <class... Args> auto f(Args...) -> void { typename x<Args...>::type v1; // OK x_t<Args...> v2; // Error x_fix_t<Args...>

Alias template, partial specialization and the invalid parameter type void

核能气质少年 提交于 2019-11-30 03:18:48
问题 Consider the following code: template<typename F> struct S; template<typename Ret, typename... Args> struct S<Ret(Args...)> { }; template<typename... Args> using Alias = S<void(Args...)>; int main() { S<void(int)> s; Alias<int> alias; } It works fine, as expected and both the line involving S and the one involving Alias define under the hood the same type S<void(int)> . Now, consider the following changes: int main() { S<void(void)> s; // this line compiles Alias<void> alias; // this line

C++11 template alias as template template argument leads to different type?

本小妞迷上赌 提交于 2019-11-29 13:27:23
We have observed a strange behaviour in the compilation of the follwing source code: template<template<class> class TT> struct X { }; template<class> struct Y { }; template<class T> using Z = Y<T>; int main() { X<Y> y; X<Z> z; z = y; // it fails here } This is a slightly modified example taken from the c++11 standard proposal for template aliases: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf (See page 4) Also note that the proposal "declares y and z to be of the same type." In our interpretation it should therefore be possible to assign (or copy construct) z from y.

How do template aliases affect template parameter deduction?

天大地大妈咪最大 提交于 2019-11-29 13:15:41
In C++03, template parameter deduction does not occur in some contexts. For example: template <typename T> struct B {}; template <typename T> struct A { typedef B<T> type; }; template <typename T> void f(typename A<T>::type); int main() { B<int> b; f(b); // ERROR: no match } Here, int is not deduced for T , because a nested type such as A<T>::type is a non-deduced context. Had I written the function like this: template <typename T> struct B {}; template <typename T> void f(B<T>); int main() { B<int> b; f(b); } everything is fine because B<T> is a deduced context. In C++11, however, template

Can I specialize a class template with an alias template?

送分小仙女□ 提交于 2019-11-28 23:34:29
Here's a simple example: class bar {}; template <typename> class foo {}; template <> using foo<int> = bar; Is this allowed? $ clang++ -std=c++0x test.cpp test.cpp:6:1: error: explicit specialization of alias templates is not permitted template <> ^~~~~~~~~~~ 1 error generated. Reference: 14.1 [temp.decls]/p3: 3 Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template. Aotium Although direct specialization of the alias is impossible, here is a workaround. (I know this is an old post but it's a useful one.) You can

C++ dynamic downcasting to class template having template template parameter being a class template or an alias template

狂风中的少年 提交于 2019-11-28 12:52:26
I hope the title makes sense. I probably miss vocabulary to express it correctly. Well, an exemple will probably be more clear. Problem for me is: dynamic downcasting returns 0 at run time in some of the following cases (written in comments). I'd like to know if it's a correct behaviour (using C++11), also why, and what can I do to make it work. Apparently, Templated and A::A_templated are treated as different classes, despite being defined as identical by using alias "using". Problem doesn't appear for simple typedef alias. template <class T> class Templated {}; class A { public : typedef int