我可以将隐式初始化重载为 0 吗?

Can I overload an implicit initialization to 0?

本文关键字:重载 初始化 我可以      更新时间:2023-10-16

是否可以编写一个类,使这些类有效:

Foo a;
Foo b = 0;
Foo c = b;
Foo d(0);
Foo e(1);
Foo f = Foo(1);

但这些不是:

int x;
Foo a = x;
Foo b = 1;
Foo c = 2;
//etc

本质上,我的规则是"常数0隐式转换为Foo,但没有其他值">

如果你不介意Foo b = nullptr;工作,很容易破解。有一个来自 int 的显式构造函数和一个来自 std::nullptr_t 的隐式构造函数。

如果你介意工作,我不确定这是否可能。区分文字0和其他整数文字的唯一方法是前者对指针和nullptr_t的隐式转换。因此,nullptr更喜欢nullptr_t参数而不是指针参数,因此通过同时使用这两个构造函数,您可以过滤掉nullptr参数。但是,0到指针和nullptr_t的转换具有相同的等级,因此这会扼杀0具有歧义的论点。

嗯。。。这样的东西可能会起作用:

class Foo {
  struct dummy;
public:
  explicit Foo(int); // the version that allows Foo x(1);
  Foo(dummy*); // the version that allows Foo x = 0;
  template <typename T,
            typename = typename std::enable_if<
                std::is_same<T, std::nullptr_t>::value>::type>
  Foo(T) = delete; // the version that prevents Foo x = nullptr;
};

我实际上还没有尝试过这个。理论上,模板应该只在参数nullptr时参与重载解析,否则SFINAE会杀死它。但是,在这种情况下,它应该比指针构造函数更好。

Foo e(1); 

将调用 Foo 的非显式构造函数,将 int 作为参数。本质上,这一行将通过尝试使用此 int 构造函数将 int 转换为 Foo 来执行相同的操作。

Foo b = 1;

您无法阻止直接处理该 int 的某些值。如果你有构造函数explicit你也无法编写下一行。

Foo b = 0;

gx_正确地指出 0 可以转换为 std::nullptr_t。以下内容将适用于您的意图。

Foo(std::nullptr_t x) : member(0) { }
explicit Foo(int c) : member(c) { }
// ...
Foo a = 0; // compiles
Foo b = 1; // doesn't compile
// Note:
int do_stuff (void) { return 0; }
Foo c = do_stuff(); // doesn't compile

我的一个想法是:

Foo(const uint32_t c) : member(0) { static_assert(c == 0, "Nope"); }
explicit Foo(uint32_t c) : member(c) { }

这是否合理?

我承认我还没有完全掌握 C++11 的右值语义,但这似乎可以做到您想要的:

class Foo
{
    public:
    Foo(int&&) {}
};
int main()
{
    Foo a(123);
    int x = 123;
    Foo b(x); // error here, line 11
    return 0;
}

结果:

prog.cpp:11:错误:无法将"int"左值绑定到"int&&">

欢迎评论,如果这段代码有任何我没有注意到的警告,或者你可以向我保证它没有。