Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a C++ constructor that accepts a variable number of int's

Tags:

Is it possible to constrain the type of arguments in a variadic constructor?

I want to be able to express

X x1(1,3,4);
X x2(3,4,5);

// syntax error: identifier 'Args'
class X {
template<int ... Args> X(Args...)
{
}
};
// this works but allows other types than int
class Y {
template<typename ... Args> Y(Args...)
{
}
};

edit to clarify intent:

What I want to achieve is to store data passed into a constructor (constants known at compile time) into a static array.

so there are some other

template<int ...values>
struct Z
{
    static int data[sizeof...(values)];
};

template<int ... values>
int Z<values...>::data[sizeof...(values)] = {values...};

and in the constructor of X I would like to use Z like this:

class X {
    template<int ... Args> X(Args...)
    {
        Z<Args...>::data // do stuff with data
    }
};

Is that possible, our do I have to use integer_sequence?

like image 846
Staffan Gustafsson Avatar asked May 20 '16 16:05

Staffan Gustafsson


People also ask

Which can accept variable number of arguments?

Only ellipses will be used to pass variable number of arguments.

What data type uses a constructor that accepts no parameters?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new .

How do you write a function with variable number of arguments in C++?

Variable number of arguments in C++Define a function with its last parameter as ellipses and the one just before the ellipses is always an int which will represent the number of arguments. Create a va_list type variable in the function definition. This type is defined in stdarg. h header file.

Can a function have variable number of parameters justify?

You can define your own methods with variable parameters, and you can specify any type for the parameters, even a primitive type. Here is a simple example: a function that computes the maximum of a variable number of values. Simply call the function like this: double m = max(3.1, 40.4, -5);

Can a constructor have parameters for structure?

Parameterized constructor inside structure in C++Parameterized constructors contain one or more than one parameter to initialize the value of the object created. It has certain properties: It does not have a return type like any other function. It has the same name as the structure name.

How many parameters are used in default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.


2 Answers

You can use std::initializer_list:

#include <iostream>
#include <initializer_list>

void myFunc(std::initializer_list<int> args)
{
    for (int i: args) std::cout << i << '\n';
}
int main(){

    myFunc({2,3,2});
    // myFunc({2,"aaa",2}); error!

}
like image 130
granmirupa Avatar answered Sep 19 '22 15:09

granmirupa


Since you have the following:

template<int... values>
struct Z
{
    static int data[ sizeof...( values ) ];
};

template <int... values>
int Z<values...>::data[ sizeof...( values ) ] = { values... };

You can use std::integer_sequence<> to pass in the ints to Z<>:

struct X
{
    template <int... values>
    X( std::integer_sequence<int, values...> )
    {
        for ( int i{ 0 }; i < sizeof...( values ); ++i )
            Z<values...>::data[ i ]; // do stuff with data
    }
};

You can make yourself a helper type to make it easy to call the ctor:

template <int... values>
using int_sequence = std::integer_sequence<int, values...>;

Then you can instantiate your class like so:

int main()
{
    X x( int_sequence<1, 3, 5>{} );
}
like image 39
user2296177 Avatar answered Sep 19 '22 15:09

user2296177