template-aliases

Unpacking parameter packs in template aliases

本小妞迷上赌 提交于 2019-12-05 13:15:43
问题 I run into a problem with unpacking variadic templates into a template alias. The following code works with Clang 3.4 and GCC 4.8 but fails with GCC 4.9: template <typename T, typename...> using front_type = T; template <typename... Ts> struct foo { using front = front_type<Ts...>; }; GCC 4.9 complains: test.cc:7:37: error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ...> using front_type = T' using front = front_type<Ts...>; ^ test.cc:1:15:

template template alias to a nested template?

倖福魔咒の 提交于 2019-12-05 02:55:31
Template aliases are very convenient in simplifying types like typename F <T>::type to just F <T> , where T and type are types. I would like to do the same for templates like F <T>::map , i.e., simplify them to F <T> , where T and map are template structs or aliases. For instance, consider the following definitions: template <bool B> using expr = std::integral_constant <bool, B>; template <bool B> using _not = expr <!B>; template <template <typename> class F> struct neg_f { template <typename T> using map = _not <F <T>{}>; }; template <typename T> pred = expr < /* ... T ... */ >; // e.g., pred

What was the issue solved by the new “using” syntax for template typedefs?

ⅰ亾dé卋堺 提交于 2019-12-04 23:03:26
In C++11 you can create a "type alias" by doing something like template <typename T> using stringpair = std::pair<std::string, T>; But this is a deviation from what you'd expect a template typedef would look like: template <typename T> typedef std::pair<std::string, T> stringpair; So this raises the question - why did they need to come up with a new syntax? what was it that did not work with the old typedef syntax? I realize the last bit doesn't compile but why can't it be made to compile? From the WG21 proposal N1489 Template aliases (by Stroustrup and Dos Reis): It has been suggested to (re

Is substitution failure an error with dependent non-type template parameters?

自闭症网瘾萝莉.ら 提交于 2019-12-04 18:46:44
问题 Let's say I have these template aliases: enum class enabler {}; template <typename T> using EnableIf = typename std::enable_if<T::value, enabler>::type; template <typename T> using DisableIf = typename std::enable_if<!T::value, enabler>::type; I can do the following in GCC: #include <iostream> template <typename T, EnableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is polymorphic\n"; } template <typename T, DisableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is not

Unpacking parameter packs in template aliases

扶醉桌前 提交于 2019-12-04 02:00:46
I run into a problem with unpacking variadic templates into a template alias. The following code works with Clang 3.4 and GCC 4.8 but fails with GCC 4.9: template <typename T, typename...> using front_type = T; template <typename... Ts> struct foo { using front = front_type<Ts...>; }; GCC 4.9 complains: test.cc:7:37: error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ...> using front_type = T' using front = front_type<Ts...>; ^ test.cc:1:15: note: declared here template <typename T, typename...> ^ There exists a filed GCC bug ( #59498 ), but is

Is substitution failure an error with dependent non-type template parameters?

允我心安 提交于 2019-12-03 12:12:35
Let's say I have these template aliases: enum class enabler {}; template <typename T> using EnableIf = typename std::enable_if<T::value, enabler>::type; template <typename T> using DisableIf = typename std::enable_if<!T::value, enabler>::type; I can do the following in GCC: #include <iostream> template <typename T, EnableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is polymorphic\n"; } template <typename T, DisableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is not polymorphic\n"; } struct foo { virtual void g() {} }; int main() { f(foo {}); f(int {}); } It prints:

Variadic template aliases as template arguments (part 2)

折月煮酒 提交于 2019-12-03 11:52:18
问题 This is a follow-up of another question. It refers to the same problem (I hope) but uses an entirely different example to illustrate it. The reason is that in the previous example only experimental GCC 4.9 failed with a compiler error. In this example, also Clang and GCC 4.8.1 fail in different ways: Clang produces an unexpected result and GCC 4.8.1 reports a different error message. Answers to the previous question say more or less that the code is valid and the problem lies with the

Variadic template aliases as template arguments (part 2)

大城市里の小女人 提交于 2019-12-03 03:17:19
This is a follow-up of another question . It refers to the same problem (I hope) but uses an entirely different example to illustrate it. The reason is that in the previous example only experimental GCC 4.9 failed with a compiler error. In this example, also Clang and GCC 4.8.1 fail in different ways: Clang produces an unexpected result and GCC 4.8.1 reports a different error message. Answers to the previous question say more or less that the code is valid and the problem lies with the experimental version of GCC. But this result makes me a bit more sceptical. I have been troubled for months

Template alias scope

孤者浪人 提交于 2019-12-02 00:12:27
问题 As per http://en.cppreference.com/w/cpp/language/type_alias, aliases are block-level declarations. It doesn't say anything special about template aliases, so it should be read that template aliases are block-level declarations as well. However, it is impossible to use template aliases at block level. The errors are different depending on the compiler - while g++ gives a meaningful message, saying that templates are not allowed at block scope, clang is completely cryptic. (example: http:/

Template alias scope

和自甴很熟 提交于 2019-12-01 20:41:13
As per http://en.cppreference.com/w/cpp/language/type_alias , aliases are block-level declarations. It doesn't say anything special about template aliases, so it should be read that template aliases are block-level declarations as well. However, it is impossible to use template aliases at block level. The errors are different depending on the compiler - while g++ gives a meaningful message, saying that templates are not allowed at block scope, clang is completely cryptic. (example: http://coliru.stacked-crooked.com/a/0f0862dad6f3da61 ). Questions I have so far: Does cppreference fail to