-3

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)

  1. ...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?

3
  • @NathanOliver It is reasonable. I have not thought about this. Write an answer. Commented Apr 8, 2021 at 12:31
  • Added. We'll see what people think. Commented Apr 8, 2021 at 12:35
  • And compilers disagree when default are only in local scope Demo... Commented Apr 8, 2021 at 12:35

2 Answers 2

3

You can only declare new default arguments in the same scope as the original declaration. using does not change this.

For non-template functions, default arguments can be added in later declarations of a function in the same scope.

dcl.fct.default/4

Sign up to request clarification or add additional context in comments.

9 Comments

Reread my question. Your answer is irrelevant.
@VladfromMoscow Found where the standard prohibits this.
using changes behavior of gcc/msvc in that case :-/
@Jarod42 msvc rejects any default parameters introduced in the using scope, which I read as the correct behaviour here
@VladfromMoscow I wasn't there when the wording was written, but "no-one specified / implemented it" is the default answer to "why does $language lack $feature"
|
2

I believe

any default argument information associated with the declaration is made known as well

doesn't mean the arguments are actually imported into the scope, it's just known that they do exist and can be used.

That would mean that void f( int = 10, int ); isn't adding to void f( int x, int y = 20 ), but is instead trying to add to void f( int, int ); which would be illegal as there isn't a default argument for the second parameter in the scope that the using declaration is in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.