variadic-templates

Implementing a std::array-like container with a C++11 initializer_list

一世执手 提交于 2019-12-05 03:53:15
The only and imo very inconvenient caveat of std::array is that it can't deduce its size from the initializer list like built-in C arrays, it's size must be passed as a template. Is it possible to implement a std::array-like container (a thin wrapper around a built-in C array) with a C++11 initializer_list? I ask because, unlike std::array, it would automatically deduce the size of the array from the initializer list which is a lot more convenient. For example: // il_array is the hypothetical container // automatically deduces its size from the initalizer list il_array <int> myarr = {2, 4, 6,

Parameter pack aware std::is_base_of()

我的梦境 提交于 2019-12-05 03:51:30
Is there a possibility to have a static assertion whether a type provided as template argument implements all of the types listed in the parameter pack ie. a parameter pack aware std::is_base_of()? template <typename Type, typename... Requirements> class CommonBase { static_assert(is_base_of<Requirements..., Type>::value, "Invalid."); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ parameter pack aware version of std::is_base_of() public: template <typename T> T* as() { static_assert(std::is_base_of<Requirements..., T>::value, "Invalid."); return reinterpret_cast<T*>(this); } }; Update for C++17: With C+

Handling zero-argument variadic template in C++11

走远了吗. 提交于 2019-12-05 03:34:24
Consider the following artificial example: template <typename T, typename... Args> struct A { typedef T Type; }; Using A with 1 or more arguments works while using it with zero arguments fails as expected: error: wrong number of template arguments (0, should be 1 or more) Is it possible to make A handle the case of zero template arguments defining A::Type to int if there are no arguments and to the first template argument if there are? First define the primary template as the most general case — which also includes zero argument: template <typename... Args> //general : 0 or more struct A {

Why is std::endl generating this cryptic error message?

和自甴很熟 提交于 2019-12-05 03:30:09
If I try to compile the following code I get the following compiler error (see code.) It compiles without error if std::endl is removed. #include <iostream> #include <sstream> #include <utility> namespace detail { template <class T> void print(std::ostream& stream, const T& item) { stream << item; } template <class Head, class... Tail> void print(std::ostream& stream, const Head& head, Tail&&... tail) { detail::print(stream, head); detail::print(stream, std::forward<Tail>(tail)...); } } template <class... Args> void print(std::ostream& stream, Args&&... args) //note: candidate function not

Variadic templates without function parameters

对着背影说爱祢 提交于 2019-12-05 03:11:30
Can I use variadic templates without using the template parameters as function parameters? When I use them, it compiles: #include <iostream> using namespace std; template<class First> void print(First first) { cout << 1 << endl; } template<class First, class ... Rest> void print(First first, Rest ...rest) { cout << 1 << endl; print<Rest...>(rest...); } int main() { print<int,int,int>(1,2,3); } But when I don't use them, it doesn't compile and complains about an ambiguity: #include <iostream> using namespace std; template<class First> void print() { cout << 1 << endl; } template<class First,

Mixing types and nontypes in variadic template parameters?

孤人 提交于 2019-12-05 02:53:59
Is it possible to do mixing of types and nontypes in variadic template parameters? If I were to pass a std::array for instance to this class as parameter T , I would need to also pass a type for the array and a length, but the way I tried it below causes an error when encountering a value, because it only expects types for Types : template < template<class, std::size_t> class T, class ... Types> class C { T<Types...> storage; }; int main(){ C<std::array, int, 3> c; } Error message: error: template argument for template type parameter must be a type Container<std::array, int, 3> c; ^ Is there a

How to unpack a variadic template parameter with a numeric sequence?

元气小坏坏 提交于 2019-12-05 02:24:33
问题 How to (or is it possible to) unpack a parameter pack with a numeric sequence? For example, template <typename C, typename... T> C* init_from_tuple(bp::tuple tpl) { return new C{bp::extract<T>("magic"(tpl))...}; // <-- } which the <-- line should expand to return new C{bp::extract<T0>(tpl[0]), bp::extract<T1>(tpl[1]), ..... bp::extract<Tn>(tpl[n])}; where n == sizeof...(T) - 1 . The purpose is to create a __init__ function for Boost.Python which accepts a tuple with predefined types. 回答1:

variadic template arguments unpacking

江枫思渺然 提交于 2019-12-05 02:12:57
问题 For each argument I need apply two nested function: obj.apply(someFilter(arg)); // arg is one argument, but here // should be an unpacking of args I don't know how to write unpacking for such case. I saw this: pass{([&]{ std::cout << args << std::endl; }(), 1)...}; on wiki, but again don't know how to apply this for my case. 回答1: It's actually quite simple: You can put arbitrary expression inside the unpack of an variadic templates argument pack: obj.apply(someFilter(arg))... This will give

Applying multiple tuples to the same function (i.e. `apply(f, tuples…)`) without recursion or `tuple_cat`

我与影子孤独终老i 提交于 2019-12-05 01:35:11
std::experimental::apply has the following signature: template <class F, class Tuple> constexpr decltype(auto) apply(F&& f, Tuple&& t); It basically invokes f by expanding t 's elements as the arguments. I would like something that does the exact same thing, but with multiple tuples at the same time: template <class F, class... Tuples> constexpr decltype(auto) multi_apply(F&& f, Tuples&&... ts); Example usage: std::tuple t0{1, 2, 3}; std::tuple t1{4, 5, 6}; auto sum = [](auto... xs){ return (0 + ... + xs); }; assert(multi_apply(sum, t0, t1) == 1 + 2 + 3 + 4 + 5 + 6); I can think of various

Using variadic templates to specify friend classes

半腔热情 提交于 2019-12-04 23:42:08
问题 I'm trying to use variadic templates to specify friend classes. I try with the following syntax, but it doesn't work. template <class... Args> struct A { friend Args...; }; I try to code some workarounds, but it seems to be not so simple since the friendship is not transitive and inherited. So the question is if there is a correct syntax or any workaround to make each individual class in Args be a friend of A? 回答1: Maybe the following CRTP variant would be sufficient for your use: template