标签: variadic

如何正确使用带有可变参数模板的引用

我有类似下面的代码:

   template<typename T1, typename T2, typename T3, typename T4>
   void inc(T1& t1, T2& t2, T3& t3, T4& t4) { ++t1; ++t2; ++t3; ++t4; }

   template<typename T1, typename T2, typename T3>
   void inc(T1& t1, T2& t2, T3& t3) { ++t1; ++t2; ++t3; }

   template<typename T1, typename T2>
   void inc(T1& t1, T2& t2) { ++t1; ++t2; }

   template<typename T1>
   void inc(T1& t1) { ++t1; }
Run Code Online (Sandbox Code Playgroud)

我想使用即将推出的标准中提出的可变参数模板重新实现它.然而,到目前为止我在网上看到的所有例子似乎都是类似于printf的例子,这里的区别似乎是引用的使用.我想出了以下内容:

inline void inc() { }

template<typename T>
inline void inc(T&& t) { ++t; }

template<typename T,typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic c++11

13
推荐指数
1
解决办法
2862
查看次数

Variadic撰写功能?

我正在尝试编写一个可变函数组合函数.基本上(.)除了第二个参数函数是可变参数之外.这应该允许表达式:

map even . zipWith (+)
Run Code Online (Sandbox Code Playgroud)

要不就

map even . zipWith
Run Code Online (Sandbox Code Playgroud)

目前,如果我添加IncoherentInstances并且需要第一个参数函数的非多态实例,我已达到的工作.

{-# LANGUAGE FlexibleInstances, OverlappingInstances, MultiParamTypeClasses, 
FunctionalDependencies, UndecidableInstances, KindSignatures #-}

class Comp a b c d | c -> d where
    comp :: (a -> b) -> c -> d

instance Comp a b (a :: *) (b :: *) where
    comp f g = f g

instance Comp c d b e => Comp c d (a -> b) (a -> e) where
    comp …
Run Code Online (Sandbox Code Playgroud)

haskell variadic pointfree function-composition

13
推荐指数
1
解决办法
883
查看次数

可变模板方法接受给定数量的双精度数?

template <unsigned int N> class myclass
{
public:
    template <typename... Args> void mymethod(Args... args)
    {
       // Do interesting stuff
    } 
};
Run Code Online (Sandbox Code Playgroud)

我希望mymethod只用N双打来打电话.那可能吗?也就是说,我说:

myclass <3> x;
x.mymethod(3., 4., 5.); // This works
x.mymethod('q', 1., 7.); // This doesn't work
x.mymethod(1., 2.); // This doesn't work
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

c++ templates variadic variadic-templates

13
推荐指数
1
解决办法
361
查看次数

有没有办法在可变参数宏参数上使用C++预处理器字符串?

我的猜测是这个问题的答案是否定的,但如果有办法的话会很棒.为了澄清,假设我有以下宏:

#define MY_VARIADIC_MACRO(X...) // Does some stuff here in the macro definition
Run Code Online (Sandbox Code Playgroud)

我想要做的是以某种方式对X的所有变量执行字符串化,然后将其传递给可变参数函数; 这里的关键字是之前的.我知道有没有办法真正从接入宏定义中的各个参数,但有没有办法字符串化所有参数,与可能类似于下面的东西?

#define MY_VARIADIC_MACRO(X...) some_variadic_function("some string", #X)
Run Code Online (Sandbox Code Playgroud)

c++ macros arguments variadic stringification

12
推荐指数
2
解决办法
8868
查看次数

C++可变参数模板不寻常的例子

以下结构意味着什么?

#include <iostream>

template <int ...> struct s;

int main() {
    int i = s<,>::xxx;

    std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

它由gcc 4.4.5+编译并在执行时输出0.

c++ gcc templates variadic c++11

12
推荐指数
1
解决办法
402
查看次数

Mixins,可变参数模板和C++中的CRTP

这是一个场景:我想拥有一个可以拥有可变数量的mixins的宿主类(对于可变参数模板来说并不太难 - 请参阅http://citeseerx.ist.psu.edu/viewdoc/summary?doi = 10.1.1.103.144).但是,我也喜欢使用主机类参数化mixins,以便它们可以引用其公共类型(使用CRTP惯用法).当试图混合两者时出现问题 - 我不清楚正确的语法.例如,以下代码无法使用g ++ 4.4.1进行编译:

template <template<class> class... Mixins>
class Host : public Mixins<Host<Mixins>>... {
  public:
    template <class... Args>
    Host(Args&&... args) : Mixins<Host>(std::forward<Args>(args))... {}
};

template <class Host> struct Mix1 {};

template <class Host> struct Mix2 {};

typedef Host<Mix1, Mix2> TopHost;
TopHost *th = new TopHost(Mix1<TopHost>(), Mix2<TopHost>());
Run Code Online (Sandbox Code Playgroud)

有错误:

tst.cpp: In constructor ‘Host<Mixins>::Host(Args&& ...) [with Args = Mix1<Host<Mix1, Mix2> >, Mix2<Host<Mix1, Mix2> >, Mixins = Mix1, Mix2]’:

tst.cpp:33:   instantiated from here

tst.cpp:18: error: type ‘Mix1<Host<Mix1, …
Run Code Online (Sandbox Code Playgroud)

g++ variadic mixins crtp c++11

11
推荐指数
1
解决办法
2185
查看次数

vararg函数如何找出机器码中的参数数量?

printf这样的可变函数如何找出它们得到的参数数量?

显然,参数的数量不会作为(隐藏)参数传递(请参阅asm示例中的printf调用).

有什么诀窍?

assembly printf variadic abi calling-convention

11
推荐指数
4
解决办法
5285
查看次数

如何为可变参数函数编写Doxygen注释,即具有未定义数量的参数的函数?

我正在尝试为具有无限数量参数的函数编写doxygen块注释,然后我找不到正确的标记.提供的参数应该都是字符串,并且它们将在函数中连接以形成新的字符串.

doxygen标签的正确用途是什么?

php comments doxygen function variadic

11
推荐指数
2
解决办法
3982
查看次数

可变参数模板的模板模板参数扩展

我最近了解了模板模板参数的存在,现在想知道这样的事情是否可行:

template<template<class... > class Container, typename... args>
struct ContainerTemplate
{
    using container = std::tuple<Container<args...>...>;
};
Run Code Online (Sandbox Code Playgroud)

我想要的是一个模板,它获取一个Container或一些其他模板类作为模板模板参数,然后扩展其余的模板参数,如果Container有N个模板参数,我给args的N*M模板参数我用N模板args获得M模板实例化,例如:

ContainerTemplate<std::vector, int, short, char>
//assuming std::vector takes only 1 arg for simplicity    
Run Code Online (Sandbox Code Playgroud)

应该导致

container = std::tuple<std::vector<int>, std::vector<short>, std::vector<char>>
Run Code Online (Sandbox Code Playgroud)

ContainerTemplate<std::map, int, int, short, short>
//assuming std::map takes only 2 args for simplicity    
Run Code Online (Sandbox Code Playgroud)

应该导致

container = std::tuple<std::map<int, int>, std::map<short, short>>
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?问题是你可以找出容器有多少模板args.

编辑:如果您需要在大小为N的元组中传递其他参数,那就没问题

ContainerTemplate<std::map, std::tuple<int, int>, std::tuple<short, short>>
Run Code Online (Sandbox Code Playgroud)

Edit2:所以我实际上找到了一种确定模板模板参数数量的方法

template<typename... T>
struct TypeList
{
    static const size_t Size = sizeof...(T);
    template<typename T2>
    struct PushFront
    {
        typedef …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic c++11

11
推荐指数
3
解决办法
1353
查看次数

C++ call function with many different types

I have an overloaded function that I have to call with many different types. The simple approach is:

uint8_t a;
uint16_t b;
//....
double x;

doSomething(a);
doSomething(b);
//...
doSomething(x);
Run Code Online (Sandbox Code Playgroud)

expressing those calls succinctly can be done with a variadic template as explained at this Q&A. The code will look somewhat like this:

auto doSomethingForAllTypes = [](auto&&... args) {
    (doSomething(args), ...);
};

uint8_t a;
uint16_t b;
//....
double x;

doSomethingForAllTypes(a, b, ... ,x);
Run Code Online (Sandbox Code Playgroud)

But I'll have to do this at …

c++ containers templates variadic

11
推荐指数
3
解决办法
994
查看次数