C++: Adding and redefinition of default arguments in real world - c++

There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example:
void foo(int a, int b, int c = -1) {
std::cout << "foo(" << a << ", " << b << ", " << c << ")\n";
}
int main() {
foo(1, 2); // output: foo(1, 2, -1)
// void foo(int a, int b = 0, int c);
// error: does not use default from surrounding scope
void foo(int a, int b, int c = 30);
foo(1, 2); // output: foo(1, 2, 30)
// void foo(int a, int b, int c = 35);
// error: we cannot redefine the argument in the same scope
// has a default argument for c from a previous declaration
void foo(int a, int b = 20, int c);
foo(1); // output: foo(1, 20, 30)
void foo(int a = 10, int b, int c);
foo(); // output: foo(10, 20, 30)
{
// in inner scopes we can completely redefine them
void foo(int a, int b = 4, int c = 8);
foo(2); // output: foo(2, 4, 8)
}
return 0;
}
Online version to play with: http://ideone.com/vdfs3t
These possibilities are regulated by the standard in 8.3.6. More specific details are in 8.3.6/4
For non-template functions, default arguments can be added in later
declarations of a function in the same scope. Declarations in
different scopes have completely distinct sets of default arguments.
That is, declarations in inner scopes do not acquire default arguments
from declarations in outer scopes, and vice versa. In a given function
declaration, each parameter subsequent to a parameter with a default
argument shall have a default argument supplied in this or a previous
declaration or shall be a function parameter pack. A default argument
shall not be redefined by a later declaration (not even to the same
value)
...
To tell the truth I never use this feature when coding in c++. I used similar code snippets several times to surprise my colleagues, but certainly not in the production code. Thus, the question is: Do you know real world examples of the code that use these features with benefits?

If you cannot change some existing code or library and you really cannot be bothered to type the correct argument then changing the default argument for some scope could be a solution.
It seems like the kind of hack that could be useful when working with C++ code generated by some legacy tool. For example, if the generated code has always had hundreds calls into some external library using a default argument but now the default argument is no longer correct.

Related

c++ variable does not name a type in macro

Have this code:
#include <iostream>
int a=0;
#define F(f) \
int t##f(int, int);\
a ++;\
int t##f(int i, int j)
F(nn) {
return i*j;
}
int main() {
int b = tnn(3, 8);
std::cout << a << b;
}
Got error when compiling:
7:3: error: 'a' does not name a type
10:1: note: in expansion of macro 'F'
Why isn't a visible in macro at the position it expands?
Look at the expansion of the macro:
F(nn) becomes
int tnn(int, int);
a++;
int tnn(int i, int j) {
return i * j;
}
The variable 'a' is being incremented outside a function which is a syntax error.
Like the other answer said, you cannot execute statements wherever you please; statements must be inside a function in order to be valid.
There are a few things that can go in the global scope:
Namespace declaration and definitions
Global variable declarations
Function prototypes and definitions
Template and class declarations and definitions
Preprocessor directives
Things that must be in function scope:
Control statements such as if and for
Labels
Function calls
Finally, the above lists are not all inclusive.
Your macro ( in the nn case) expands to:
int a=0;
int tnn(int, int); a ++; int tnn(int i, int j) {
return i*j;
}
int main() {
int b = tnn(3, 8);
std::cout << a << b;
}
There is no global scope in C++. That is only in scripting languages.
Execution order is an initialization library-- something like crt0.s which
constructs your run time envioronment. Then initialize global variables ( this part can get very complicated ) then run main.
You statement fails simply because you cannot put arbitrariy executable code where the macro is expanded.
PS: Bjarne says don't use macros. In fact he create const, inline and to some degree templates so that you can avoid macros. Macros are evil!!!!

default values in functions used in for_each

I tried to use for_each with boost::trim. In the first place I used a wrong code
std::for_each(v.begin(),v.end(),&boost::trim<std::string>));
// error: too few arguments to function
then I fixed (reading online) with this
std::for_each(v.begin(),v.end()
,boost::bind(&boost::trim<std::string>,_1,std::locale()));
How the compiler works when it needs to pass this function to for_each. I thought that since std::locale is the default parameter for the second input argument of boost::trim my code should have worked.
Default arguments are applied when you call a function, but they don't form part of the function signature. In particular, when you call a function through a function pointer, you have generally lost the information which default arguments are available:
void (*f)(int, int);
void foo(int a, int b = 20);
void bar(int a = 10, int = -8);
f = rand() % 2 == 0 ? foo : bar;
f(); // ?
The upshot is that to use bind on f you will always need to populate both parameters.
You can always write it using a lambda:
std::for_each(v.begin(), v.end(), [](std::string & s) { boost::trim(s); });
Now compiler will have enough knowledge to use default parameters.

How can I cleanly specify which arguments I am passing and which remain default?

Asked because of this: Default argument in c++
Say I have a function such as this: void f(int p1=1, int p2=2, int p3=3, int p4=4);
And I want to call it using only some of the arguments - the rest will be the defaults.
Something like this would work:
template<bool P1=true, bool P2=true, bool P3=true, bool P4=true>
void f(int p1=1, int p2=2, int p3=3, int p4=4);
// specialize:
template<>
void f<false, true, false, false>(int p1) {
f(1, p1);
}
template<>
void f<false, true, true, false>(int p1, int p2) {
f(1, p1, p2);
}
// ... and so on.
// Would need a specialization for each combination of arguments
// which is very tedious and error-prone
// Use:
f<false, true, false, false>(5); // passes 5 as p2 argument
But it requires too much code to be practical.
Is there a better way to do this?
Use the Named Parameters Idiom (→ FAQ link).
The Boost.Parameters library (→ link) can also solve this task, but paid for by code verbosity and greatly reduced clarity. It's also deficient in handling constructors. And it requires having the Boost library installed, of course.
Have a look at the Boost.Parameter library.
It implements named paramaters in C++. Example:
#include <boost/parameter/name.hpp>
#include <boost/parameter/preprocessor.hpp>
#include <iostream>
//Define
BOOST_PARAMETER_NAME(p1)
BOOST_PARAMETER_NAME(p2)
BOOST_PARAMETER_NAME(p3)
BOOST_PARAMETER_NAME(p4)
BOOST_PARAMETER_FUNCTION(
(void),
f,
tag,
(optional
(p1, *, 1)
(p2, *, 2)
(p3, *, 3)
(p4, *, 4)))
{
std::cout << "p1: " << p1
<< ", p2: " << p2
<< ", p3: " << p3
<< ", p4: " << p4 << "\n";
}
//Use
int main()
{
//Prints "p1: 1, p2: 5, p3: 3, p4: 4"
f(_p2=5);
}
Although Boost.Parameters is amusing, it suffers (unfortunately) for a number of issues, among which placeholder collision (and having to debug quirky preprocessors/template errors):
BOOST_PARAMETER_NAME(p1)
Will create the _p1 placeholder that you then use later on. If you have two different headers declaring the same placeholder, you get a conflict. Not fun.
There is a much simpler (both conceptually and practically) answer, based on the Builder Pattern somewhat is the Named Parameters Idiom.
Instead of specifying such a function:
void f(int a, int b, int c = 10, int d = 20);
You specify a structure, on which you will override the operator():
the constructor is used to ask for mandatory arguments (not strictly in the Named Parameters Idiom, but nobody said you had to follow it blindly), and default values are set for the optional ones
each optional parameter is given a setter
Generally, it is combined with Chaining which consists in making the setters return a reference to the current object so that the calls can be chained on a single line.
class f {
public:
// Take mandatory arguments, set default values
f(int a, int b): _a(a), _b(b), _c(10), _d(20) {}
// Define setters for optional arguments
// Remember the Chaining idiom
f& c(int v) { _c = v; return *this; }
f& d(int v) { _d = v; return *this; }
// Finally define the invocation function
void operator()() const;
private:
int _a;
int _b;
int _c;
int _d;
}; // class f
The invocation is:
f(/*a=*/1, /*b=*/2).c(3)(); // the last () being to actually invoke the function
I've seen a variant putting the mandatory arguments as parameters to operator(), this avoids keeping the arguments as attributes but the syntax is a bit weirder:
f().c(3)(/*a=*/1, /*b=*/2);
Once the compiler has inlined all the constructor and setters call (which is why they are defined here, while operator() is not), it should result in similarly efficient code compared to the "regular" function invocation.
This isn't really an answer, but...
In C++ Template Metaprogramming by David Abrahams and Aleksey Gurtovoy (published in 2004!) the authors talk about this:
While writing this book, we reconsidered the interface used for named
function parameter support. With a little experimentation we
discovered that it’s possible to provide the ideal syntax by using
keyword objects with overloaded assignment operators:
f(slew = .799, name = "z");
They go on to say:
We’re not going to get into the implementation details of this named
parameter library here; it’s straightforward enough that we suggest
you try implementing it yourself as an exercise.
This was in the context of template metaprogramming and Boost::MPL. I'm not too sure how their "straighforward" implementation would jive with default parameters, but I assume it would be transparent.

Boost.Fusion Functional: Calling functions with default arguments

Is it possible to use boost::fusion::invoke function to call a function that has default arguments without specifying those?
Example:
void foo(int x, int y = 1, int z = 2)
{
std::cout << "The sum is: " << (x + y + z) << std::endl;
}
...
// This should call foo(0). It doesn't work because the type of foo is void (*) (int, int, int).
boost::fusion::invoke(foo, boost::fusion::vector<int>(0));
// Works
boost::fusion::invoke(foo, boost::fusion::vector<int, int, int>(0, 1, 2));
I am writing a wrapper for bindings to a scripting language and default arguments would greatly improve the intuitive feel for the users of the wrapper. I am afraid though that the standard has not covered this case.
A side note:
I know one could work around it using functors:
struct foo {
void operator() (int x, int y = 1, int z = 2) { /* ... */ }
};
// Works because the functor adds an indirection
boost::fusion::invoke(foo(), boost::fusion::vector<int>(0));
This is however not an option as I don't want to force the users to create functors just to specify default arguments.
You can use bind (more info):
boost::fusion::invoke(boost::bind(foo, _1, 1, 2), boost::fusion::vector<int>(0));

is it possible in C or C++ to create a function inside another?

Could someone please tell me if this is possible in C or C++?
void fun_a();
//int fun_b();
...
main(){
...
fun_a();
...
int fun_b(){
...
}
...
}
or something similar, as e.g. a class inside a function?
thanks for your replies,
Wow, I'm surprised nobody has said yes! Free functions cannot be nested, but functors and classes in general can.
void fun_a();
//int fun_b();
...
main(){
...
fun_a();
...
struct { int operator()() {
...
} } fun_b;
int q = fun_b();
...
}
You can give the functor a constructor and pass references to local variables to connect it to the local scope. Otherwise, it can access other local types and static variables. Local classes can't be arguments to templates, though.
C++ does not support nested functions, however you can use something like boost::lambda.
C — Yes for gcc as an extension.
C++ — No.
you can't create a function inside another function in C++.
You can however create a local class functor:
int foo()
{
class bar
{
public:
int operator()()
{
return 42;
}
};
bar b;
return b();
}
in C++0x you can create a lambda expression:
int foo()
{
auto bar = []()->int{return 42;};
return bar();
}
No but in C++0x you can http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions which may take another few years to fully support. The standard is not complete at the time of this writing.
-edit-
Yes
If you can use MSVC 2010. I ran the code below with success
void test()
{
[]() { cout << "Hello function\n"; }();
auto fn = [](int x) -> int { cout << "Hello function (" << x << " :))\n"; return x+1; };
auto v = fn(2);
fn(v);
}
output
Hello function
Hello function (2 :))
Hello function (3 :))
(I wrote >> c:\dev\loc\uniqueName.txt in the project working arguments section and copy pasted this result)
The term you're looking for is nested function. Neither standard C nor C++ allow nested functions, but GNU C allows it as an extension. Here is a good wikipedia article on the subject.
Clang/Apple are working on 'blocks', anonymous functions in C! :-D
^ ( void ) { printf("hello world\n"); }
info here and spec here, and ars technica has a bit on it
No, and there's at least one reason why it would complicate matters to allow it. Nested functions are typically expected to have access to the enclosing scope. This makes it so the "stack" can no longer be represented with a stack data structure. Instead a full tree is needed.
Consider the following code that does actually compile in gcc as KennyTM suggests.
#include <stdio.h>
typedef double (*retdouble)();
retdouble wrapper(double a) {
double square() { return a * a; }
return square;
}
int use_stack_frame(double b) {
return (int)b;
}
int main(int argc, char** argv) {
retdouble square = wrapper(3);
printf("expect 9 actual %f\n", square());
printf("expect 3 actual %d\n", use_stack_frame(3));
printf("expect 16 actual %f\n", wrapper(4)());
printf("expect 9 actual %f\n", square());
return 0;
}
I've placed what most people would expect to be printed, but in fact, this gets printed:
expect 9 actual 9.000000
expect 3 actual 3
expect 16 actual 16.000000
expect 9 actual 16.000000
Notice that the last line calls the "square" function, but the "a" value it accesses was modified during the wrapper(4) call. This is because a separate "stack" frame is not created for every invocation of "wrapper".
Note that these kinds of nested functions are actually quite common in other languages that support them like lisp and python (and even recent versions of Matlab). They lead to some very powerful functional programming capabilities, but they preclude the use of a stack for holding local scope frames.
void foo()
{
class local_to_foo
{
public: static void another_foo()
{ printf("whatevs"); }
};
local_to_foo::another_foo();
}
Or lambda's in C++0x.
You can nest a local class within a function, in which case the class will only be accessible to that function. You could then write your nested function as a member of the local class:
#include <iostream>
int f()
{
class G
{
public:
int operator()()
{
return 1;
}
} g;
return g();
}
int main()
{
std::cout << f() << std::endl;
}
Keep in mind, though, that you can't pass a function defined in a local class to an STL algorithm, such as sort().
int f()
{
class G
{
public:
bool operator()(int i, int j)
{
return false;
}
} g;
std::vector<int> v;
std::sort(v.begin(), v.end(), g); // Fails to compile
}
The error that you would get from gcc is "test.cpp:18: error: no matching function for call to `sort(__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >, f()::G&)'
"
It is not possible to declare a function within a function. You may, however, declare a function within a namespace or within a class in C++.
Not in standard C, but gcc and clang support them as an extension. See the gcc online manual.
Though C and C++ both prohibit nested functions, a few compilers support them anyway (e.g., if memory serves, gcc can, at least with the right flags). A nested functor is a lot more portable though.
No nested functions in C/C++, unfortunately.
As other answers have mentioned, standard C and C++ do not permit you to define nested functions. (Some compilers might allow it as an extension, but I can't say I've seen it used).
You can declare another function inside a function so that it can be called, but the definition of that function must exist outside the current function:
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char* argv[])
{
int foo(int x);
/*
int bar(int x) { // this can't be done
return x;
}
*/
int a = 3;
printf( "%d\n", foo(a));
return 0;
}
int foo( int x)
{
return x+1;
}
A function declaration without an explicit 'linkage specifier' has an extern linkage. So while the declaration of the name foo in function main() is scoped to main(), it will link to the foo() function that is defined later in the file (or in a another file if that's where foo() is defined).