简体   繁体   English

使用声明和 function 默认 arguments

[英]The using declaration and function default arguments

According to the C++ 17 Standard (10.3.3 The using declaration)根据 C++ 17 标准(10.3.3 使用声明)

1 Each using-declarator in a using-declaration98 introduces a set of declarations into the declarative region in which the using-declaration appears. 1 using-declaration98 中的每个 using-declarator 都将一组声明引入到 using-declaration 出现的声明性区域中。

and

10 A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed. 10 using-declaration 是一种声明,因此可以在允许(且仅在)允许多个声明的情况下重复使用。

and (The C++ 17 Standard, 11.3.6 Default arguments)和(C++ 17 标准,11.3.6 默认参数)

  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. ...当通过使用声明 (10.3.3) 引入 function 的声明时,与声明相关的任何默认参数信息也会被告知。 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. 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编译成功,其 output 为

x = 10, y = 20

So could be the same principles applied to functions introduced by the using declaration?那么,同样的原则是否可以应用于 using 声明引入的函数?

What is the reason of that such an addition of default arguments is not allowed?不允许这样添加默认 arguments 的原因是什么?

You can only declare new default arguments in the same scope as the original declaration.您只能在与原始声明相同的 scope 中声明新的默认 arguments。 using does not change this. using不会改变这一点。

For non-template functions, default arguments can be added in later declarations of a function in the same scope.对于非模板函数,可以在同一 scope 中的 function 的后续声明中添加默认 arguments。

dcl.fct.default/4

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.并不意味着arguments实际上被导入到scope中,只是知道它们确实存在并且可以使用。

That would mean that void f( int = 10, int );这意味着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 );没有添加到void f( int x, int y = 20 ) ,而是尝试添加到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.这将是非法的,因为使用声明所在的 scope 中的第二个参数没有默认参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM