A class method with the number of arguments specified by the integer pattern parameter

It’s not entirely accurate how to formulate this question or what to look for, so if this is the same as another question, please close and redirect the corresponding question.

Let's pretend that

template<typename Type, int Size> class vector
{
  Type data[Size];
}

Is it possible to replace a constructor that takes the value of the number of arguments in specialized templates, such as

template<typename Type> class vector3<Type,3>
{
  Type data[3];
  public:
    vector3( Type, Type, Type );
}

with something in a non-specific class of templates? Like the "varargs constructor" that creates a constructor with the size of the type argument number?

A solution including C ++ 0x features is great.

+3
source share
4 answers

In C ++ 0x you have template typedeffinally got it!

: ...

:

template< typename second>
using TypedefName = SomeType<OtherType, second, 5>;

template <class Type>
using vector3 = vector<Type, 3>;

, ;)

. , , . , static_assert .

.

template <class Type, size_t Size>
class vector
{
public:
  template <class... Args>
  vector(Args... args): data({args...})
  {
    // Necessary only if you wish to ensure that the exact number of args
    // is passed, otherwise there could be less than requested
    BOOST_MPL_ASSERT_RELATION(sizeof...(Args), ==, Size);
  }

private:
  T data[Size];
};

, , boost::enable_if.

template <class Type, size_t Size>
class vector
{
public:
  vector(Type a0, typename boost::enable_if_c< Size == 1 >::type* = 0);
  vector(Type a0, Type a1, typename boost::enable_if_c< Size == 2 >::type* = 0);
  // ...
};

Boost.Preprocessor .

BOOST_PP_REPEAT(MAX_COUNT, CONSTRUCTOR_MACRO, ~);

// where MAX_COUNT is defined to the maximum size you wish
// and CONSTRUCTOR_MACRO actually generates the constructor

#define CONSTRUCTOR_MACRO(z, n, data)                              \
  vector(                                                          \
    BOOST_PP_ENUM_PARAMS(n, Type a),                               \
    typename boost::enable_if_c< Size == n >::type* = 0            \
  );

. BOOST_PP_REPEAT.

, , , .

+5

:

template<typename T, unsigned int N>
struct vector {
    T data[N];

    template<typename... Args>
    vector(Args... args) : data({args...}) { }
};

N, T.

+3

, ,

, , . : boost:: tuple ( , ).

++ 0x variadic templates.

+2

std::array. , , , , . , , 2 , 3.

template< typename T>
using Vector3 = std::array<T,3>;

Vector3 v1{1,2,3};
Vector3 v2{1,2}; // it sounds like you want to disallow this case.

, std::array, .

template<typename T, std::size_t SIZE>
class Vector
{
public:
   template< typename ... Args >
   Vector( Args ... args ) :
      data({args...})
   {
      static_assert( sizeof...(Args) == SIZE,
                     "Incorrect number of arguments passed to Vector constructor");
   }
   /* lots of extra code here to add std::array -like methods */
private:
   // could use std::array here as well.
   T data[3];
};

template< typename T >
using Vector3 = Vector<T,3>;

Vector3 v1(1,2,3);
Vector3 v2(1,2); // error at compile time.
+2

Source: https://habr.com/ru/post/1754836/


All Articles