If I have a prototype that looks like this:
function(float,float,float,float)
I can pass values like this:
function(1,2,3,4);
So if my prototype is this:
function(float*);
Is there any way I can achieve something like this?
function( {1,2,3,4} );
Just looking for a lazy way to do this without creating a temporary variable, but I can't seem to nail the syntax.
This is marked both C and C++, so you're gonna get radically different answers.
If you are expecting four parameters, you can do this:
But that is rather unsafe, as you could do this by accident:
It is usually best to leave them as individual parameters. Since you shouldn't be using raw arrays anyway, you can do this:
An improvement, but not much of one. You could use
boost::array
, but rather than an error for mismatched size, they are initialized to 0:This will all be fixed in C++0x, when initializer list constructors are added:
And probably
boost::array
as well:The bad news is that there is no syntax for that. The good news is that this will change with the next official version of the C++ standard (due in the next year or two). The new syntax will look exactly as you describe.
you can write a builder class that would allow for about the same syntax
this way, you can use the class as
foo( const std::vector & v);
foo( Builder< std::vector >(1)(2)(3)(4) );
You can create a compound literal:
Although, I'm not sure why you want to go through the trouble. This is not permitted by ISO.
Generally, shortcuts like this should be avoided in favor of readability in all cases; laziness is not a good habit to explore (personal opinion, of course)
No, you cannot do that. I do not have the standard available here, so I cannot give an exact reference, but the closest thing to what you ask for is string constants, i.e.
is treated by the compiler as
There is no way for other types of variables to be treated this way.
To add to the fun, you can use templates to make it variable in length.