variadic-templates

User defined literals for variadic char template

我的未来我决定 提交于 2019-12-10 11:37:18
问题 Recently, in the gcc-trunk sources was implemented the "user defined literals". Tell me please, do I understand correctly that I can`t define a "user defined literals" for variadic char template? template<char... chars> int operator"" _call() { return sizeof...(chars); } ... std::cout << "method"_call; Up. I don`t understand why this expression is allowed: template<char... chars> int operator"" _call() { return sizeof...(chars); } ... std::cout << 12345566_call; and this one is disallowed:

C++ polymorphism with variadic function parameter

馋奶兔 提交于 2019-12-10 10:13:53
问题 I am sharing with you an issue that I got with a class using variadic function parameters. It is the class Thread shown in the following code. It is a wrapper of std::thread in order to use the function pattern. I wanted to use polymorphism with this function in inheriting the class Thread into a new class, Functor, but gcc returns the errors bellow: #include <thread> #include <iostream> using namespace std; template<class... Args> class Thread { public: virtual void operator()(Args...) = 0;

Calling std::make_tuple with lambdas in a variadic template - is this supposed to work in C++11?

梦想与她 提交于 2019-12-10 10:10:10
问题 in C++11 a lambda function is an object, and it should be possible to call make_tuple with it, right? void foobar() { auto t = std::make_tuple([](){ std::make_shared<int>(); }); } This code works for me. Now what happens if we add a variadic template: #include <tuple> #include <memory> template <class... T> void foobar() { auto t = std::make_tuple([](){ std::make_shared<T>(); }...); } int main(int, char**) { foobar<int, float, double>(); return 0; } This one fails to compile in GCC 4.7.2 main

variadic templates with template function names and passing arguments and return values around

穿精又带淫゛_ 提交于 2019-12-10 10:03:03
问题 following from this question, I have been trying to create a template function that calls all same-named methods of its mixins. This has been done and verified in the previous question. Now I am attempting to get the return value of SensorType:: Analytically: #include<iostream> #include <string> struct EdgeSensor { void update(int i) { std::cout << "EdgeSensor::update " << i << std::endl; } void updat2(const int i ) { std::cout << "EdgeSensor::updat2" << i << std::endl; } std::string

Is it possible to get the first type of a parameter pack in a one-liner?

浪子不回头ぞ 提交于 2019-12-10 09:53:38
问题 I have a parameter pack given in a variadic template class and want to extract the first type. Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityType should be defined to have the type of the first type in EntityTs . Note, I want to keep the signature of the class template. I know that it would be possible to write template<typename FirstEntityType, typename... OtherEntityTypes> , it is however something that I don't want

bind make_shared with variadic template

霸气de小男生 提交于 2019-12-10 09:35:34
问题 I'm trying to write the following factory class, but I can't find the proper syntax: template<class T, typename... TArgs> class Factory { public: Factory(TArgs... args) { creator_ = std::bind(&std::make_shared<T, TArgs...>, args...); // ^^^ some error around here } std::shared_ptr<T> Create() const { return creator_(); } private: std::function<std::shared_ptr<T>()> creator_; }; This is how I use the factory: class Foo { public: Foo(bool value) {} }; class Bar { public: Bar(const std::string&

Could not convert from brace-enclosed initializer list to std tuple

匆匆过客 提交于 2019-12-10 03:28:58
问题 As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support

How to create a variadic template string formatter

别说谁变了你拦得住时间么 提交于 2019-12-10 03:06:58
问题 We need to format strings all the time. It would be so nice to be able to say: std::string formattedStr = format("%s_%06d.dat", "myfile", 18); // myfile_000018.dat Is there a C++ way of doing this? Some alternatives I considered: snprintf : uses raw char buffers. Not nice in modern C++ code. std::stringstream : does not support format pattern strings, instead you must push clumsy iomanip objects into the stream. boost::format : uses an ad-hoc operator overload of % to specify the arguments.

Parameter pack aware std::is_base_of()

青春壹個敷衍的年華 提交于 2019-12-10 02:46:54
问题 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

Mixing types and nontypes in variadic template parameters?

浪子不回头ぞ 提交于 2019-12-10 02:30:24
问题 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: