variadic-templates

Member function traits

Deadly 提交于 2019-12-12 11:31:32
问题 I am writing a template class that wraps around member functions to reduce some calls - if some condition is true, the member function doesn't need to be called. The signature would look something like this template <typename MemFuncType, MemFuncType> class MemberWrapper; And I can specialize it thus: template <typename R, typename T, R T::* MemFunc> class MemberWrapper<R T::*, MemFunc>{}; I would also like to restrict the number of arguments of R T::* . How do I do this? The only solution I

variadic template pack within decltype

喜欢而已 提交于 2019-12-12 11:02:59
问题 I may write as template< class T0> struct Last0 { using type = decltype(T0{}); // OK compiles. `type = T0` }; template< class T0, class T1> struct Last1 { using type = decltype(T0{}, T1{}); // OK, compiles. `type = T1` }; template< class T0, class T1, class T2> struct Last3{ using type = decltype(T0{}, T1{}, T2{}); // Ok, compiles. `type = T2` }; But, when I use variadic templates, it's not compiled: template< class ... T> struct Last{ using type = decltype(T{} ... ); //<--- Error !!! }; What

Why does the following program not select the argument of the same type as the first template parameter?

依然范特西╮ 提交于 2019-12-12 10:45:41
问题 I am trying to write a function such that f<T>(args..) returns the first parameter of type T . The following program seems to always select the first specialization thus printing 97 (ASCII code of 'a' ). Though the second one wouldn't require converting char to int . Could someone please explain the behavior? I am new to SFINAE and meta-programming. #include <iostream> using namespace std; template <typename T, typename ...Ts> T f(T a, Ts... args) { return a; } template <typename R, typename

Template arguments deduction for function parameter pack followed by other parameters

眉间皱痕 提交于 2019-12-12 10:39:02
问题 Is the deduction for f1 and f2 ill-formed? template<class... T, class U> void f1(T..., U){} template<class... T> void f2(T..., int){} int main() { f1(1); f2(1); return 0; } g++ accepts both, clang only accepts f2 , and msvc rejects both. Related standard wording: [temp.deduct.call] When a function parameter pack appears in a non-deduced context ([temp.deduct.type]), the type of that parameter pack is never deduced. [temp.deduct.type]p5 The non-deduced contexts are: A function parameter pack

int a[] { (functioncall(a1, a2), 0)…}; (void(a)); What does this syntax do/mean?

霸气de小男生 提交于 2019-12-12 09:57:32
问题 I came across this post variadic template function to concatenate std::vector containers suggesting the use of the following syntax: template<typename T> void append_to_vector(std::vector<T>& v1, const std::vector<T>& v2) { std::cout << v2[0] << std::endl; for (auto& e : v2) v1.push_back(e); } template<typename T, typename... A> std::vector<T> concat_version3(std::vector<T> v1, const A&... vr) { int unpack[] { (append_to_vector(v1, vr), 1)... }; (void(unpack)); return v1; } I started playing

C++ handle/body class automatically composed from template parameter pack. Can this be improved?

此生再无相见时 提交于 2019-12-12 09:52:48
问题 Problem: Multiple components wish to share the same immutable data (of arbitrary type) but each component has a different set of interface requirements. For example, component A may require that there is a free function available called hash_code(const object&) and component B may require that the object has a free function available called type(const object&) . If component C makes a shared pointer to some object O, for which there is a free function available called hash_code and a free

Why won't template parameter pack be deduced to multiple type arguments in function call?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 07:46:00
问题 I have a class templated on a type parameter and parameter pack, and am confused about type-deduction of this type; while writing an output-streaming operator I discovered a parameter pack on operator<< will not match both the type and pack parameters for the template class: #include <iostream> template<class T, class... Ts> struct foo { /* ... */ }; template< class... Ts > std::ostream& operator<<( std::ostream& os, const foo<Ts...>& ) { return os << 42; } int main() { std::cout << foo<int>(

Compare two sets of types for equality

為{幸葍}努か 提交于 2019-12-12 07:14:37
问题 How can one check if two parameter packs are the same, ignoring their internal order? So far I only have the frame (using std::tuple ), but no functionality. #include <tuple> #include <type_traits> template <typename, typename> struct type_set_eq : std::false_type { }; template <typename ... Types1, typename ... Types2> struct type_set_eq<std::tuple<Types1...>, std::tuple<Types2...>> : std::true_type { // Should only be true_type if the sets of types are equal }; int main() { using t1 = std:

replacing the n-th element from a variadic template list

ε祈祈猫儿з 提交于 2019-12-12 04:25:01
问题 I tried to use THIS ANSWER to get the following working: (replacing the n-th element from a variadic list and packing it as a tuple) template<typename... Ts> using pack_as_tuple = std::tuple<Ts...>; template< std::size_t N, typename T, typename... Ts> struct replace_nth_type_in_list { typedef replace_nth_type<N,T, pack_as_tuple<Ts...>> type; }; int main() { using U = std::tuple<std::string,unsigned,size_t,double>; using rep0 = replace_nth_type<0,char,U>::type; using rep1 = replace_nth_type<1

variadic templated Object multiplication

一世执手 提交于 2019-12-11 20:39:55
问题 In the below code I am doing multiplication variadic templates for the int values and also for the Objects. It works for the all primitive types. It also works for the only 2 objects. But the code doesn't compile when I use more than 2 arguments objects into multiply. multiply(1, 2, 3, 4, 5, 6) //works correcyly multiply( A(1), B(1)) //works correctly multiply( A(1), B(1), B(1) ); //compile time Error multiply( A(1), B(1), B(1), B(1) ); //compile time Error How could I solve this problem for