According to the C++ 17 Standard (10.3.3 The using declaration)
1 Each using-declarator in a using-declaration98 introduces a set of declarations into the declarative region in which the using-declaration appears.
and
10 A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed.
and (The C++ 17 Standard, 11.3.6 Default arguments)
- ...When a declaration of a function is introduced by way of a using-declaration (10.3.3), any default argument information associated with the declaration is made known as well. If the function is redeclared thereafter in the namespace with additional default arguments, the additional arguments are also known at any point following the redeclaration where the using-declaration is in scope.
So this program
#include <iostream>
void f( int x, int y = 20 )
{
std::cout << "x = " << x << ", y = " << y << '\n';
}
int main()
{
using ::f;
void f( int, int );
f( 10 );
return 0;
}
as expected compiles and outputs
x = 10, y = 20
In fact it is similar to the program
#include <iostream>
void f( int x, int y )
{
std::cout << "x = " << x << ", y = " << y << '\n';
}
int main()
{
void f( int, int = 20 );
void f( int, int );
f( 10 );
return 0;
}
Now it would be logical consistent that the following program also was valid.
#include <iostream>
void f( int x, int y = 20 )
{
std::cout << "x = " << x << ", y = " << y << '\n';
}
int main()
{
using ::f;
void f( int, int );
f( 10 );
void f( int = 10, int );
f();
return 0;
}
However this program does not compile.
On the other hand, consider the following program.
#include <iostream>
namespace N
{
int a = 10;
int b = 20;
void f( int, int = b );
}
int a = 30;
int b = 40;
void N::f( int x = a, int y )
{
std::cout << "x = " << x << ", y = " << y << '\n';
}
int main()
{
using N::f;
f();
return 0;
}
It compiles successfully and its output is
x = 10, y = 20
So could be the same principles applied to functions introduced by the using declaration?
What is the reason of that such an addition of default arguments is not allowed?