如何使用类模板参数来更改参数调用和函数签名? [英] How to use class template argument to change argument calls and function signatures?
问题描述
我试图找到一种方法来设计类模板,以便传递 int
值,并且几个函数签名以及参数列表都依赖于该值。
I am trying to find way to design a class template so that an int
value is passed, and several function signatures as well as argument lists are dependent on this value.
特别是考虑 MyClass
:
template <int N>
class MyClass {
typedef SomeType<int, int, int, /* ... N times*/ > MyDepType;
myFunction(std::string arg0, std::string arg1, /* ...*/ std::string argN) { /* do stuff */};
public:
MyClass() {
someFunction(float arg0, float arg1, /* ...*/ float argN); // <
someOtherFunction(boost::bind(&MyClass::myFunction, this, _1, _2, /*...*/ _N));
};
};
我希望能够表达私有typedef调用和<$ c的签名$ c> myFunction 并将参数列表传递给外部函数 someFunction
和 someOtherFunction
我无法编辑/重写。
是否可以使用C ++ 11标准来实现这一目标?
I'd like to be able to express both the private typedef call, the signature of myFunction
and the argument list passed to external functions someFunction
and someOtherFunction
, which I can't edit/rewrite.
Is there a way to achieve this, using the C++11 standard?
推荐答案
您可以使用 std :: index_sequence
(C ++ 14,但可以在SO上找到C ++ 11的实现):
You might use std::index_sequence
(C++14, but implementation in C++11 can be found on SO):
template <std::size_t, typename T>
using always_t = T;
template <std::size_t ... Is>
class MyClassImpl {
using MyDepType = SomeType<always_t<Is, int>...>;
void myFunction(always_t<Is, std::string>... args) { /* do stuff */}
public:
MyClass() {
void someFunction(always_t<Is, std::float>... args);
someOtherFunction([this](always_t<Is, const std::string&>... args) {
return myFunction(args...);
});
//
std::array<std::string, sizeof...(Is)> strings = {std::to_string(Is)...};
myFunction(strings[Is]...);
};
};
template <std::size_t N>
using MyClass = MyClassImpl<std::make_index_sequence<N>>;
这篇关于如何使用类模板参数来更改参数调用和函数签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!