gpt4 book ai didi

c++ - 统一初始化中的尾随逗号

转载 作者:IT老高 更新时间:2023-10-28 22:04:42 24 4
gpt4 key购买 nike

在统一初始化期间使用尾随逗号时是否存在任何潜在的语义差异?

std::vector< std::size_t > v1{5, }; // allowed syntax
std::vector< std::size_t > v2{10};

我可以使用尾随逗号使编译器选择std::vector::vector(std::initializer_list< std::size_t >)吗?构造函数而不是 std::vector::vector(std::size_t, const std::size_t &)或者还有其他提到语法的技巧吗?

我可以用它来检测是否有std::initializer_list - 构造函数重载?

考虑以下代码,必须选择哪个构造函数?

struct A { A(int) { ; } A(double, int = 3) { ; } };
A a{1};
A b{2, };

此代码 is accepted by gcc 8 A(int)在这两种情况下都被选中。

最佳答案

首先,C++ 语法规则使尾随 , braced-init-list 是可选的。报价 dcl.init/1

A declarator can specify an initial value for the identifier being declared. The identifier designates a variable being initialized. The process of initialization described in the remainder of [dcl.init] applies also to initializations specified by other syntactic contexts, such as the initialization of function parameters ([expr.call]) or the initialization of return values ([stmt.return]).

initializer:
brace-or-equal-initializer
( expression-list )
brace-or-equal-initializer:
= initializer-clause
braced-init-list
initializer-clause:
assignment-expression
braced-init-list
braced-init-list:
{ initializer-list ,opt }
{ designated-initializer-list ,opt }
{ }

其次,您几乎无法覆盖重载解决系统。它将始终使用 std::initializer_list构造函数,如果你使用这样的语法和这样的std::initializer_list构造函数可用。

dcl.init.list/2 :

A constructor is an initializer-list constructor if its first parameter is of type std​::​initializer_­list or reference to possibly cv-qualified std​::​initializer_­list for some type E, and either there are no other parameters or else all other parameters have default arguments. [ Note: Initializer-list constructors are favored over other constructors in list-initialization ([over.match.list])....


下面的程序打印 Using InitList :

#include <iostream>
#include <initializer_list>

struct X{
X(std::initializer_list<double>){ std::cout << "Using InitList\n"; }
X(int){ std::cout << "Using Single Arg ctor\n"; }
};

int main(){
X x{5};
}

尽管 5int 类型的文字,选择单参数构造函数应该是有意义的,因为它是完美匹配的;和 std::initializer_list<double>构造函数需要 double 的列表.然而,规则有利于 std::initializer_list<double>因为它是一个 initializer-list 构造函数

结果,即使是下面的程序也因为缩小转换而失败:

#include <iostream>
#include <initializer_list>

struct Y{
Y(std::initializer_list<char>){ std::cout << "Y Using InitList\n"; }
Y(int, int=4){ std::cout << "Y Using Double Arg ctor\n"; }
};

int main(){
Y y1{4777};
Y y2{577,};
Y y3{57,7777};
}

针对您在下面的评论,“如果 std::initializer_list 没有重载,或者它不是第一个构造函数的参数怎么办?” - 然后重载决议不会选择它。演示:

#include <iostream>
#include <initializer_list>

struct Y{
Y(int, std::initializer_list<double>){ std::cout << "Y Using InitList\n"; }
Y(int, int=4){ std::cout << "Y Using Double Arg ctor\n"; }
};

int main(){
Y y1{4};
Y y2{5,};
Y y3{5,7};
}

打印:

Y Using Double Arg ctor
Y Using Double Arg ctor
Y Using Double Arg ctor

如果没有 initializer-list 构造函数 可用,那么 {initializer-list...,}根据 dcl.init/16,初始化程序几乎可以退回到直接初始化。 ,其语义由 dcl.init/16 的前一段涵盖

关于c++ - 统一初始化中的尾随逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com