variadic-templates

C++11 variadic templates and comma-separated expressions equivalence

别说谁变了你拦得住时间么 提交于 2019-12-23 12:24:52
问题 In a variadic template the ... operator expands a parameter pack into a series of comma-separated arguments (in the simplest form). My question is: how come that calling some_function() for multiple arguments comma-separated works and calling it with the ... operator doesn't? I'm talking about this code: template<typename... Args> inline void expand(Args&&... args) { some_function(22),some_function(32); // Works some_function(args)...; // Doesn't work - ERROR } Shouldn't these two lines

What is the Type This struct is Inheriting From?

情到浓时终转凉″ 提交于 2019-12-23 10:59:09
问题 So this example from: http://en.cppreference.com/w/cpp/utility/variant/visit declares the specialized type: template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; Which is constructed as an r-value here: std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); I'm

What is the Type This struct is Inheriting From?

陌路散爱 提交于 2019-12-23 10:58:38
问题 So this example from: http://en.cppreference.com/w/cpp/utility/variant/visit declares the specialized type: template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; Which is constructed as an r-value here: std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); I'm

variadic template arguments: can I pick reference vs value depending on type?

三世轮回 提交于 2019-12-23 10:57:09
问题 edit This is not a duplicate of Undefined reference to static class member. That question explored the cause of the problem (which I explain below). Here, I'm looking for a different solution from those proposed in the answers to that questions (which implied changing the declaration/definition of the constexpr variable to be used -- essentially by adding a definition in a compilation unit). I have created a little variadic template function make_string() to generate a std::string from any

Variadic Template Functions: No matching function for call, std::endl

我是研究僧i 提交于 2019-12-23 09:01:14
问题 In my code, I use variadic template functions for the logging purpose. But when I use std::endl as parameter, I get the following compiler error: Error: no matching function for call to 'LOG_ERROR(const char [14], int&, )' LOG_ERROR("Sum of x+y = ", z, std::endl); note: candidate: 'void LOG_ERROR()' inline void LOG_ERROR() { note: candidate expects 0 arguments, 3 provided My Code: #include <iostream> inline void LOG_ERROR() { std::cout << std::endl; } template<typename First, typename ...Rest

Interaction between default arguments and parameter pack (GCC and clang disagree)

蹲街弑〆低调 提交于 2019-12-23 08:53:54
问题 I expect the following code to compile: #include <iostream> template <class Tag = void, class T = int, class... Args> void print(T val = T{}, Args... args) { std::cout << val << ' ' << sizeof...(args) << std::endl; } int main() { print(); print(3.14); print(0, 1, 2); } While it compiles on GCC 5.2 (C++11) despite the unused-but-set-parameter warnings, clang 3.6 (C++11) gives the following error messages: main.cpp:4:33: error: missing default argument on parameter 'args' void print(T val = T{}

Variadic template base class call forwarding

自古美人都是妖i 提交于 2019-12-23 08:05:30
问题 In pre-11 C++ I had something like this: template<class T,class U,class V> struct Foo : T,U,V { bool init() { if(!T::init() || !U::init() || !V::init()) return false; // do local init and return true/false } }; I'd like to convert this to C++11 variadic syntax to get the benefit of the flexible length argument list. I understand the concept of unpacking the template arg list using recursion but I just can't seen to get the syntax right. Here's what I've tried: template<typename... Features>

Variadic template base class call forwarding

与世无争的帅哥 提交于 2019-12-23 08:05:08
问题 In pre-11 C++ I had something like this: template<class T,class U,class V> struct Foo : T,U,V { bool init() { if(!T::init() || !U::init() || !V::init()) return false; // do local init and return true/false } }; I'd like to convert this to C++11 variadic syntax to get the benefit of the flexible length argument list. I understand the concept of unpacking the template arg list using recursion but I just can't seen to get the syntax right. Here's what I've tried: template<typename... Features>

std::endl and variadic template [duplicate]

▼魔方 西西 提交于 2019-12-23 07:47:22
问题 This question already has answers here : Cannot have typeof(std::endl) as template parameter? (2 answers) Closed 5 years ago . Code: #include <iostream> void out() { } template<typename T, typename... Args> void out(T value, Args... args) { std::cout << value; out(args...); } int main() { out("12345", " ", 5, "\n"); // OK out(std::endl); // compilation error return 0; } Build errors: g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "..

Mixing variadic template values and variadic deduced types

心不动则不痛 提交于 2019-12-23 07:37:12
问题 Is the following perfectly defined by the standard ? #include <iostream> template <unsigned int... Values, class... Types> void f(Types&&... values) { std::cout<<sizeof...(Values)<<" "<<sizeof...(Types)<<std::endl; } int main() { f<7, 5>(3); return 0; } It compiles well under g++ 4.8 but I wonder if it is normal. 回答1: From ISO C++ standard's current working draft 14.1 (11): A template parameter pack of a function template shall not be followed by another template >parameter unless that