variadic-templates

Necessity of forward-declaring template functions

时光毁灭记忆、已成空白 提交于 2019-12-10 00:45:01
问题 I recently created this example code to illustrate C++11 variadic template function usage. template <typename Head, typename... Tail> void foo (Head, Tail...); template <typename... Tail> void foo (int, Tail...); void foo () {} template <typename... Tail> void foo (int x, Tail... tail) { std :: cout << "int:" << x; foo (tail...); } template <typename Head, typename... Tail> void foo (Head x, Tail... tail) { std :: cout << " ?:" << x; foo (tail...); } foo (int (123), float (123)); // Prints

“Folding” of template parameter packs in pre C++17: idiomatic approach

别说谁变了你拦得住时间么 提交于 2019-12-09 23:45:57
问题 Fold-ish expressions in C++11 & C++14: idiomatic approach? The accepted answer of the Q&A Variadic template pack expansion makes use of a common pre-C++17 (prior to fold expressions) approach to "folding" of an unexpanded template parameter pack. I've seen a few different variations of this technique; taking the Q&A above as an example: #include <initializer_list> #include <iostream> #include <utility> template <typename T> static void bar(T) {} template <typename... Args> static void foo1

g++ and clang++ (with libc++) different behaviour with template template class specialization

梦想与她 提交于 2019-12-09 18:33:59
问题 I'm playing with c++11 and I came across a difference in behavior between g++ 4.9.2 and clang++ 3.5 (but only when it uses the libc++; when it uses the libstdc++, clang++ seems to behave as such as g++) related with a template template specialization for a template class. The following is a trivial example #include <set> template <typename X> class foo; template <template<typename, typename ...> class C, typename X> class foo<C<X>> {}; int main () { foo<std::set<int>> f; return 0; } g++

Variadic template parameters of one specific type

半城伤御伤魂 提交于 2019-12-09 17:53:54
问题 Why there is no specific type allowed in a variadic template pack? template< typename T > class Foo { public: template< typename... Values > void bar( Values... values ) { } template< T... values > <-- syntax error void bar( T... values ) { } template< int... values > <-- syntax error void bar( int... values ) { } }; Whats the rationale in not allowing this? Are there proposals for this? Note: alternatives would be std::initializer_list< T > without narrowing of types and the { } -brace

Multikey map using variadic templates

◇◆丶佛笑我妖孽 提交于 2019-12-09 17:38:46
问题 I'm trying to implement a map with different access keys using variadic templates in c++. What I want to get is to make such syntax work: MultikeyMap<int, double, float> map1; // int and double are keys, float is value type map1[ 2 ] = 3.5; map1[ 5.7 ] = 22; MultikeyMap<unsigned long long, int, float, double, int> map2; // more keys, int is value type map2[100000000000ULL] = 56; // etc... What I have now looks like: template<class V, class... Krest> class MultikeyMap; template<class V, class

Variadic version of std::is_convertible?

痴心易碎 提交于 2019-12-09 15:59:30
问题 Is it possible to write a variadic version of std::is_convertible ? For example are_convertible<T1, T2, T3, T4> would return is_convertible<T1, T3> && is_convertible<T2, T4> . I've been thinking about this for a few hours but couldn't come up with anything reasonable. To clarify I want to use it somewhat like this: template <class ...Args1> struct thing { template <class ...Args2> enable_if_t<are_convertible<Args2..., Args1...>::value> foo(Args2 &&...args){} } 回答1: You don't need to

Are checked guard parameter packs cause of ill-formed programs in case of specializations?

℡╲_俬逩灬. 提交于 2019-12-09 15:55:53
问题 This is a follow-up on this question. Consider the following code: #include <type_traits> template<typename T, typename... P, typename U = std::enable_if_t<std::is_integral<T>::value>> void f() { static_assert(sizeof...(P) == 0, "!"); } int main() { f<int>(); } It compiles, but according to [temp.res]/8 it is ill-formed, no diagnostic required because of: every valid specialization of a variadic template requires an empty template parameter pack Now consider this slightly different example:

Create a std::function type with limited arguments

半腔热情 提交于 2019-12-09 12:31:28
问题 Given the type of a callable function C , I want to get at compile time a std::function ; the type of which: has the same return type of function C the argument types are the first N argument types of function C This means that, for a given type void(int, char, double) and a given N , the type of the function is: N = 1 => result type: std::function<void(int)> N = 2 => result type: std::function<void(int, char)> N = 3 => result type: std::function<void(int, char, double)> N > 3 => compile time

Intel C++ Compiler is extremely slow to compile recursive decltype returns

此生再无相见时 提交于 2019-12-09 12:01:32
问题 I'm writing a template for expressions parametrised by an arbitrary number of char labels. Given an argument list, a factory function returns an expression of different types depending on whether there are two arguments of the same types or whether they are unique. A concrete example: suppose that A is a "labelable" object with its operator() overloaded to produce an ?Expression<...> . Let a, b, ... be declared as labels LabelName<'a'>, LabelName<'b'>, ... . Then A(a,b,c,d) would produce a

c++11: building a std::tuple from a template function

非 Y 不嫁゛ 提交于 2019-12-09 11:44:49
问题 I have the following function: template<class T> T Check(int index); How can I write a function, CheckTuple , which, given a tuple type, populates a tuple with calls to Check ? For example: CheckTuple< std::tuple<int, float, std::string> >() would return the following tuple: std::make_tuple( Check<int>(1), Check<float>(2), Check<std::string>(3) ) The other questions I see involve unpacking a given tuple, not building one up this way. 回答1: Implementing what you're looking for becomes pretty