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?
Only ellipses will be used to pass variable number of arguments.
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 .
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.
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);
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.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.
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!
}
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>{} );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With