我正在尝试存储std::tuple不同数量的值,这些值稍后将用作调用与存储类型匹配的函数指针的参数.
我创建了一个简化的示例,显示了我正在努力解决的问题:
#include <iostream>
#include <tuple>
void f(int a, double b, void* c) {
std::cout << a << ":" << b << ":" << c << std::endl;
}
template <typename ...Args>
struct save_it_for_later {
std::tuple<Args...> params;
void (*func)(Args...);
void delayed_dispatch() {
// How can I "unpack" params to call func?
func(std::get<0>(params), std::get<1>(params), std::get<2>(params));
// But I *really* don't want to write 20 versions of dispatch so I'd rather
// write something like:
func(params...); // Not legal
}
}; …Run Code Online (Sandbox Code Playgroud) c++ function-pointers variadic-templates c++11 iterable-unpacking
为什么std::make_unique标准C++ 11库中没有函数模板?我发现
std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3));
Run Code Online (Sandbox Code Playgroud)
有点冗长.以下不是更好吗?
auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
这隐藏得new很好,只提到一次类型.
无论如何,这是我尝试实现make_unique:
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
我花了很std::forward长时间来编译这些东西,但我不确定它是否正确.是吗?究竟是什么std::forward<Args>(args)...意思?编译器对此做了什么?
在浏览gcc当前新C++ 11标头的实现时,我偶然发现了"......"令牌.您可以检查,以下代码编译正常 [通过ideone.com].
template <typename T>
struct X
{ /* ... */ };
template <typename T, typename ... U>
struct X<T(U......)> // this line is the important one
{ /* ... */ };
Run Code Online (Sandbox Code Playgroud)
那么,这个令牌的含义是什么?
编辑:看起来如此修剪"......"的问题标题为"......",我的意思是"......".:)
我只是编写一个通用对象工厂并使用boost预处理器元库来创建一个可变参数模板(使用2010并且它不支持它们).我的函数使用rval引用并std::forward进行完美的转发,它让我思考...当C++ 0X出来并且我有一个标准编译器时,我会使用真正的可变参数模板.但是,我怎么称呼std::forward这些论点?
template <typename ...Params>
void f(Params... params) // how do I say these are rvalue reference?
{
y(std::forward(...params)); //? - I doubt this would work.
}
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一方法就是需要手动拆包... params,我也不是那里.是否有更快的语法可行?
c++ rvalue-reference variadic-templates perfect-forwarding c++11
在C++ 11中有像这样的可变参数模板:
template< class T, class... Args >
unique_ptr<T> make_unique( Args&&... args )
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
有一些好奇这个问题:表达std::forward<Args>(args)...同时使用Args和args,但只有一个...令牌.此外,std::forward是一个非可变参数模板函数,只接受一个模板参数和一个参数.那个(大致)的语法规则是什么?如何概括?
另外:在函数实现中,省略号(...)位于感兴趣的表达式的末尾.有没有理由在模板参数列表和参数列表中省略号位于中间?
是否可以以某种方式存储参数包供以后使用?
template <typename... T>
class Action {
private:
std::function<void(T...)> f;
T... args; // <--- something like this
public:
Action(std::function<void(T...)> f, T... args) : f(f), args(args) {}
void act(){
f(args); // <--- such that this will be possible
}
}
Run Code Online (Sandbox Code Playgroud)
然后是:
void main(){
Action<int,int> add([](int x, int y){std::cout << (x+y);}, 3, 4);
//...
add.act();
}
Run Code Online (Sandbox Code Playgroud) 这是我之前关于漂亮印刷STL容器的问题的后续行动,为此我们设法开发了一个非常优雅且完全通用的解决方案.
在下一步中,我想std::tuple<Args...>使用可变参数模板包含漂亮打印(因此这是严格的C++ 11).因为std::pair<S,T>,我只是说
std::ostream & operator<<(std::ostream & o, const std::pair<S,T> & p)
{
return o << "(" << p.first << ", " << p.second << ")";
}
Run Code Online (Sandbox Code Playgroud)
打印元组的类似结构是什么?
我已经尝试了各种模板参数堆栈解包,传递索引并使用SFINAE来发现我何时处于最后一个元素,但没有成功.我不会用破碎的代码给你带来负担; 问题描述有希望直截了当.基本上,我想要以下行为:
auto a = std::make_tuple(5, "Hello", -0.1);
std::cout << a << std::endl; // prints: (5, "Hello", -0.1)
Run Code Online (Sandbox Code Playgroud)
与前一个问题包含相同级别的通用性(char/wchar_t,pair delimiters)的加分点!
前提:
在稍微使用了可变参数模板之后,我意识到实现稍微超出简单的元编程任务的任何东西很快变得非常麻烦.特别是,我发现自己希望的执行方式上的参数包一般操作如迭代,拆分,循环在一个std::for_each样的方式,等等.
在观看了由安德烈Alexandrescu的本次讲座由C++和超越2012年的愿望static if成C++(从借来构建d编程语言),我的感觉是某种static for会来得心应手,以及-我觉得更多的这些的static结构能带来好处.
所以我开始想知道是否有办法为变量模板函数(伪代码)的参数包实现类似的东西:
template<typename... Ts>
void my_function(Ts&&... args)
{
static for (int i = 0; i < sizeof...(args); i++) // PSEUDO-CODE!
{
foo(nth_value_of<i>(args));
}
}
Run Code Online (Sandbox Code Playgroud)
哪个会在编译时被翻译成这样的东西:
template<typename... Ts>
void my_function(Ts&&... args)
{
foo(nth_value_of<0>(args));
foo(nth_value_of<1>(args));
// ...
foo(nth_value_of<sizeof...(args) - 1>(args));
}
Run Code Online (Sandbox Code Playgroud)
原则上,static_for将允许更精细的处理:
template<typename... Ts>
void foo(Ts&&... args)
{
constexpr s …Run Code Online (Sandbox Code Playgroud) c++ iteration template-meta-programming variadic-templates c++11
如何计算可变参数模板函数的参数数量?
即:
template<typename... T>
void f(const T&... t)
{
int n = number_of_args(t);
...
}
Run Code Online (Sandbox Code Playgroud)
number_of_args在上面实现的最佳方法是什么?
当我偶然发现这个问题时,我正在尝试使用C++ 0x可变参数模板:
template < typename ...Args >
struct identities
{
typedef Args type; //compile error: "parameter packs not expanded with '...'
};
//The following code just shows an example of potential use, but has no relation
//with what I am actually trying to achieve.
template < typename T >
struct convert_in_tuple
{
typedef std::tuple< typename T::type... > type;
};
typedef convert_in_tuple< identities< int, float > >::type int_float_tuple;
Run Code Online (Sandbox Code Playgroud)
当我尝试输入模板参数包时,GCC 4.5.0给出了一个错误.
基本上,我想将参数包"存储"在typedef中,而无需解压缩.可能吗?如果没有,是否有一些理由不允许这样做?