小编pre*_*ous的帖子

为什么Foo({})调用Foo(0)而不是Foo()?

由代码中的clang 3.5.0和gcc 4.9.1生成的可执行文件

#include <iostream>

struct Foo
{
   Foo() { std::cout << "Foo()" << std::endl; }
   Foo(int x) { std::cout << "Foo(int = " << x << ")" << std::endl; }
   Foo(int x, int y) { std::cout << "Foo(int = " << x << ", int = " << y << ")" << std::endl; }
};

int main()                 // Output
{                          // ---------------------
   auto a = Foo();         // Foo()
   auto b = Foo(1);        // Foo(int = 1)
   auto c = …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer value-initialization list-initialization c++14

15
推荐指数
1
解决办法
1164
查看次数

重载分辨率:首选直接转换运算符(由于复制删除)?

给定

struct E
{
};

struct P
{
    explicit P(E) {}
};

struct L
{
    operator E() {return {};}
    operator P() {return P{E{}};}
};
Run Code Online (Sandbox Code Playgroud)

根据C ++ 17语言标准,该表达式应该P{L{}}编译吗?

不同的编译器产生不同的结果:

  • gcc(trunk):好的
  • gcc 8.3:错误(超载不明确)
  • gcc 7.4:好的
  • lang声(trunk):好的
  • 铛8.0.0:好
  • 铛7.0.0:好的
  • msvc v19.20:错误(不明确的过载)
  • icc 19.0.1:错误(多个构造函数实例匹配)

c++ language-lawyer overload-resolution copy-elision c++17

10
推荐指数
1
解决办法
176
查看次数

为什么braced-init-list在函数调用和构造函数调用中表现不同?

使用clang 3.5.0和gcc 4.9.1编译以下代码会在最后一个语句中产生错误.

#include <iostream>

struct Foo { Foo(int x, int y) { std::cout << "Foo(int = " << x << ", int = " << y << ")" << std::endl; } };

void bar(int x, int y) { std::cout << "bar(int = " << x << ", int = " << y << ")" << std::endl; }

int main()
{
   Foo({}, {});   // Foo(int = 0, int = 0)
   Foo({1}, {2}); // Foo(int = 1, int = 2)
   Foo({1, …
Run Code Online (Sandbox Code Playgroud)

c++ constructor function list-initialization c++14

5
推荐指数
1
解决办法
131
查看次数