每个可变参数模板参数生成一个类成员

use*_*674 28 c++ template-meta-programming variadic-templates c++11

我有一个模板类,其中每个模板参数代表内部计算可以处理的一种类型的值.需要模板(而不是函数重载),因为值作为boost :: any传递,并且它们的类型在运行时之前不清楚.

为了正确地转换为正确的类型,我想为每个可变参数类型都有一个成员列表,如下所示:

template<typename ...AcceptedTypes> // e.g. MyClass<T1, T2>
class MyClass {
    std::vector<T1> m_argumentsOfType1;
    std::vector<T2> m_argumentsOfType2; // ...
};
Run Code Online (Sandbox Code Playgroud)

或者,我想将模板参数类型存储在列表中,以便用它做一些RTTI魔术(?).但是如何将它们保存在std :: initializer_list成员中对我来说也不清楚.

谢谢你的帮助!

Eth*_*ris 21

正如您已经暗示的那样,最好的方法是使用元组:

template<typename ...AcceptedTypes> // e.g. MyClass<T1, T2>
class MyClass {
    std::tuple<std::vector<AcceptedTypes>...> vectors;
};
Run Code Online (Sandbox Code Playgroud)

这是乘以"字段"的唯一方法,因为你不能神奇地拼写字段名称.另一个重要的事情可能是获得一些命名访问权限.我想你想要实现的是拥有多个具有唯一类型的向量,因此您可以通过其值类型"搜索"正确的向量:

template <class T1, class T2>
struct SameType
{
    static const bool value = false;
};

template<class T>
struct SameType<T, T>
{
    static const bool value = true;
};

template <typename... Types>
class MyClass
{
     public:
     typedef std::tuple<vector<Types>...> vtype;
     vtype vectors;

     template<int N, typename T>
     struct VectorOfType: SameType<T,
        typename std::tuple_element<N, vtype>::type::value_type>
     { };

     template <int N, class T, class Tuple,
              bool Match = false> // this =false is only for clarity
     struct MatchingField
     {
         static vector<T>& get(Tuple& tp)
         {
             // The "non-matching" version
             return MatchingField<N+1, T, Tuple,
                    VectorOfType<N+1, T>::value>::get(tp);
         }
     };

     template <int N, class T, class Tuple>
     struct MatchingField<N, T, Tuple, true>
     {
        static vector<T>& get(Tuple& tp)
        {
            return std::get<N>(tp);
        }
     };

     template <typename T>
     vector<T>& access()
     {
         return MatchingField<0, T, vtype,
                VectorOfType<0, T>::value>::get(vectors);
     }
};
Run Code Online (Sandbox Code Playgroud)

这是测试用例,所以你可以尝试一下:

int main( int argc, char** argv )
{
    int twelf = 12.5;
    typedef reference_wrapper<int> rint;

    MyClass<float, rint> mc;
    vector<rint>& i = mc.access<rint>();

    i.push_back(twelf);

    mc.access<float>().push_back(10.5);

    cout << "Test:\n";
    cout << "floats: " << mc.access<float>()[0] << endl;
    cout << "ints: " << mc.access<rint>()[0] << endl;
    //mc.access<double>();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的任何类型都不是您传递给myClass的类型列表(请参阅此注释的双重访问),您将收到编译错误,不太可读,但gcc至少指向正确的位置导致问题,至少这样的错误消息表明问题的正确原因 - 例如,如果您尝试执行mc.access <double>():

 error: ‘value’ is not a member of ‘MyClass<float, int>::VectorOfType<2, double>’
Run Code Online (Sandbox Code Playgroud)


Ric*_*ges 5

在πάντα-ῥεῖ的评论中提到的一种做这种事情的方法是使用元组.他没有解释的(可能是为了拯救你自己)是看起来如何.

这是一个例子:

using namespace std;

// define the abomination    
template<typename...Types>
struct thing
{
    thing(std::vector<Types>... args)
    : _x { std::move(args)... }
    {}

    void print()
    {
        do_print_vectors(std::index_sequence_for<Types...>());
    }

private:
    template<std::size_t... Is>
    void do_print_vectors(std::index_sequence<Is...>)
    {
        using swallow = int[];
        (void)swallow{0, (print_one(std::get<Is>(_x)), 0)...};
    }

    template<class Vector>
    void print_one(const Vector& v)
    {
        copy(begin(v), end(v), ostream_iterator<typename Vector::value_type>(cout, ","));
        cout << endl;
    }

private:
    tuple<std::vector<Types>...> _x;
};


// test it
BOOST_AUTO_TEST_CASE(play_tuples)
{
    thing<int, double, string> t {
        { 1, 2, 3, },
        { 1.1, 2.2, 3.3 },
        { "one"s, "two"s, "three"s }
    };

    t.print();
}
Run Code Online (Sandbox Code Playgroud)

预期产量:

1,2,3,
1.1,2.2,3.3,
one,two,three,
Run Code Online (Sandbox Code Playgroud)


sha*_*hay 5

一种不使用元组的替代解决方案是使用CRTP创建一个类层次结构,其中每个基类都是其中一种类型的特化:

#include <iostream>
#include <string>

template<class L, class... R> class My_class;

template<class L>
class My_class<L>
{
public:

protected:
  L get()
  {
    return val;
  }

  void set(const L new_val)
  {
    val = new_val;
  }

private:
  L val;
};

template<class L, class... R>
class My_class : public My_class<L>, public My_class<R...>
{
public:
  template<class T>
  T Get()
  {
    return this->My_class<T>::get();
  }

  template<class T>
  void Set(const T new_val)
  {
    this->My_class<T>::set(new_val);
  }
};

int main(int, char**)
{
  My_class<int, double, std::string> c;
  c.Set<int>(4);
  c.Set<double>(12.5);
  c.Set<std::string>("Hello World");

  std::cout << "int: " << c.Get<int>() << "\n";
  std::cout << "double: " << c.Get<double>() << "\n";
  std::cout << "string: " << c.Get<std::string>() << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)