C++函数具有无限参数但具有相同(固定)类型

Nar*_*rek 2 c++

我想要一个具有无限数量参数的函数,但我也想确保那些都是相同类型的指针.像这样的东西:

void myFunc(float value, MyClass* ....)
{
    // take all pointers of type MyClass and call function `f` like this: a->(value);
    // store pointer in a vector like: vector.push_back(a);
}
Run Code Online (Sandbox Code Playgroud)

我可以用C++实现吗?

Yak*_*ont 7

void myFunc(float value, std::initializer_list<MyClass*> il){
  for(auto* p:il)
    p->f(value);
}
Run Code Online (Sandbox Code Playgroud)

不会发生堆/免费商店分配.

使用是 myFunc(3.14, {ptr1,ptr2,ptr3});

如果你真的讨厌,{}你可以使用不受限制的模板转发到上面.在转发点,将进行类型检查.SFINAE可用于更早地进行类型检查.

template<class...MyClasses>
void myFunc2( float value, MyClasses*... ps) {
  myFunc( value, {ps...} );
}
Run Code Online (Sandbox Code Playgroud)

(可能名称更改)

或者,可以完成基于SFINAE的完整解决方案,直接调用p->f,就像使用火箭筒来处理垃圾一样.当然,垃圾将会消失,但这仍然是一个坏主意.

初始化列表旨在有效地捆绑相同类型的参数.

现在,询问您的MyClass*请求的一个好问题是......为什么要关心?如果传入的参数兼容->f(3.14f),为什么不调用->f(3.14f)?这就是为什么实际问题是更好的问题而不是抽象的问题:问题的最佳解决方案因实际问题而异.

火箭筒解决方案如下所示.

首先,一个小模板元编程库:

// Better name for the type:
template<bool b>
using bool_t = std::integral_constant<bool, b>;
// bundle of types:
template<class...>struct types{using type=types;};

// takes a test, and a types<?...> of things to test against it:
template<template<class...>class Z, class args>
struct test_lists:std::true_type{};
template<template<class...>class Z, class...Ts, class...Us>
struct test_lists<Z,types<types<Ts...>,Us...>>:bool_t<
  Z<Ts...>{} && test_lists<Z, types<Us...>>{}
>{};

// takes 0 or more types<?...> and concatenates them:
template<class...types>
struct concat_types;
template<class...types>
using concat_types_t=typename concat_types<types...>::type;
template<>
struct concat_types<>:types<>{};
template<class...Ts>
struct concat_types<types<Ts...>>:types<Ts...>{};
template<class...T0s,class...T1s, class...more>
struct concat_types<types<T0s...>,types<T1s...>,more...>:
  concat_types_t< types<T0s...,T1s...>, more... >
{};

// takes a template Z and and arg, and produces a template
// equal to Z<Arg, ?...>:
template<template<class...>class Z, class Arg>
struct bind_1st {
    template<class...Ts>
    using apply=Z<Arg,Ts...>;
};

// takes a template Z and a types<?...> and produces
// types< Z<?>... >:
template<template<class...>class Z, class types>
struct map;
template<template<class...>class Z, class types>
using map_t=typename map<Z,types>::type;
template<template<class...>class Z, class...Ts>
struct map<Z,types<Ts...>>:types<Z<Ts>...>{};

// builds a cross product of zero or more types<?...>:
template<class...types0>
struct cross_types;
template<class...types>
using cross_types_t=typename cross_types<types...>::type;

// valid degenerate cases:
template<class...Ts>
struct cross_types<types<>,Ts...>:types<>{};
template<>
struct cross_types<>:types<types<>>{};

// meat of cross_types:
template<class T0, class...T0s, class...Us>
struct cross_types<types<T0,T0s...>, Us...>:
  concat_types_t<
    map_t< bind_1st< concat_types_t, types<T0> >::template apply, cross_types_t<Us...> >,
    cross_types_t< types<T0s...>, Us... >
  >
{};

// takes a test Z, and a sequence of types<?...> args
// tests the cross product of the contents of the args:
template<template<class...>class Z, class...Args>
struct test_cross : test_lists<Z, cross_types_t<Args...>> {};
Run Code Online (Sandbox Code Playgroud)

这一点之上的一切都是通用的元编程代码.你可以更直接地完成下一部分,但上面的通用元编程代码可以用于其他类似的问题,它确实使后面的东西"更清晰".

// a toy MyClass type to test against:
struct MyClass {
    void f(float x){
        std::cout << x << '\n';
    }
};

// Simple SFINAE test that the types passed in are exactly
// pointers to MyClass:
template<class...Ts>
std::enable_if_t<
  test_cross<std::is_same, types<MyClass>, types<Ts...>>{}
>
myFunc( float x, Ts*... p ) {
  using discard=int[];
  (void)discard{0,((
    p->f(x)
  ),void(),0)...};
}
Run Code Online (Sandbox Code Playgroud)

请注意,这std::is_base_of可能是一个更好的选择is_same.

核心在这里:

  test_cross<std::is_same, types<MyClass>, types<Ts...>>{}
Run Code Online (Sandbox Code Playgroud)

这会评估std::is_same<A,B>每一对<MyClass, Ts>.

一个更容易实现的方法就是一个模板,它带走了一大堆bool...&&在它们上面做了一起std::is_same<MyClass, Ts>{}....但我喜欢编写元编程库,并且简洁地进行n路交叉产品测试是一个更有趣的问题.

实例

基于python的堆栈溢出答案的交叉产品