简体   繁体   English

如何以简单的方式声明可变参数模板类的类型

[英]How to declare the type of a variadic templated class in a simple way

I have a variadic templated struct in VS2013 that uses a templated function to allow automatic type deduction. 我在VS2013中有一个可变的模板化结构,该结构使用模板化功能来允许自动类型推断。

template<typename... T> struct BitVector {
   BitVector(T... args){...}
}
using BitVector_t = struct BitVector<T...>;

template<typename... T>
std::shared_ptr<BitVector_t<T...>>
CreateBitVector(T... args) {
   auto v = new BitVector_t<T...>(args...);
   return std::shared_ptr<BitVector_t<T...>(v);
}

With this, I can define the items and defaultvalues of my bitvector in one single line. 这样,我可以在一行中定义我的位向量的项和默认值。 Furthermore I can easily create a BitVector by calling 此外,我可以通过调用轻松创建BitVector

auto mybitvector = CreateBitVector("Item1", int(1), "Item2", std::string("defval")...);

where the types are deduced from the function parameters. 从函数参数推导类型。 This works great with auto , but if I need a certain BitVector as a class member, where no auto is allowed, all efforts to have a clean and simple API, where types and default values are just specified at a single location, seem to be gone, as a class member would require an additional declaration like 这对于auto很有效 ,但是如果我需要某个BitVector作为类成员,则不允许使用auto ,那么所有努力来获得一个干净简单的API,即只在单个位置指定类型和默认值,消失了,因为一个类成员将需要一个额外的声明,例如

std::shared_ptr<
BitVector<char const*, int, 
char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>> 
mMyBitVector;

Can anyone imagine a simple workaround, to get the member declaration, without writing the parameter types explicitly a second time? 谁能想象一个简单的解决方法来获取成员声明,​​而无需第二次显式编写参数类型?

As suggested in comment, decltype may help: 如评论中所建议, decltype可能会有所帮助:

#define AUTO_MEMBER(memberName, init) decltype(init) memberName = (init)

class C
{
public:    
    //...
private:
    AUTO_MEMBER(mMyBitVector, CreateBitVector("Item1", 1, "Item2", std::string("defval")));
};

Demo 演示版

but I would prefer be explicit on member type (and may be using some typedef ). 但我希望在成员类型上明确(并且可能使用一些typedef )。

Create a type alias with all the template parameters. 使用所有模板参数创建一个类型别名。

using MyBitVector_t = std::shared_ptr<BitVector<char const*, int, std::string>>;

Use the type alias to declare a member. 使用类型别名声明成员。

MyBitVector_t mMyBitVector;

The type alias can be used to define the return types of functions and types of arguments of functions too. 类型别名也可以用于定义函数的返回类型和函数的参数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将每个类型包装在模板化类中的可变参数模板中 - Wrapping each type in a variadic template in a templated class 从模板化模板类和可变参数模板中声明“容器”对象 - Declare “container” object from templated template class and variadic templates 如何为模板化嵌套类类型的函数声明参数? - How do I declare an argument for a function of a templated nested class type? 如何转发声明属于类的模板化类型? - How to forward declare templated type that should belong to a class? 如何声明模板类的析构函数 - How to declare destructor of a templated class 如何专门化在可变参数模板 class 中定义的可变参数方法? - How to specialized a variadic argument method defined inside a variadic templated class? 在每种类型的可变参数模板上使用 class 初始化元组 - Initialize tuple with class templated on each type of variadic template 获得可变模板类的第N个参数的最简单方法是什么? - Easiest way to get the N-th argument of a variadic templated class? 如何将模板化的struct / class声明为朋友? - How to declare a templated struct/class as a friend? 如何向可变参数类声明模板友元函数 - how to declare a template friend function to a variadic class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM