有没有办法禁用在类上的构造函数合成? [英] Is there a way to disable constructor synthesizing on a class?

查看:196
本文介绍了有没有办法禁用在类上的构造函数合成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类,我想确保我的编译器(在这种情况下,GCC)不合成任何构造函数或赋值方法。我发现了一种方法来做到这一点,只是在类中包含一个const int成员,但这不会擦伤我。

解决方案

如果您定义(或仅声明)它自己,然后编译器不会为你定义它。

  struct A 
{
A ); / *声明就足以防止编译器从
生成默认构造函数!* /
};

虽然声明足以阻止编译器生成默认构造函数,



在C ++ 11(新的ISO标准)中,你的代码需要默认的构造函数,否则你会遇到链接器错误。可以禁用构造函数,复制构造函数和复制赋值为:

  struct A 
{
(const A&)= delete // disable copy-constructor
A& operator =(const A&)= delete; // disable copy-assignment
};






现在有趣的部分



您也可以选择性地禁用所选类型的构造函数,使 delete 更有趣。考虑这个,

  struct A 
{
A(int){}
};

此类的对象不仅可以使用 int 参数,但任何隐式转换为 int 的类型。例如,

  A a1(10); // ok 
A a2('x'); // ok - char可以隐式转换为int

B b;
A a3(b); //确定 - 假设b提供用户定义的转换为int

现在假设,不希望类 A 的用户创建具有 char B类的对象,幸运或不幸的是可以隐式地转换为 int ,那么您可以禁用它们:

  struct A 
{
A(int){}
A // disable
A(const B&)= delete; // disable
};

现在这里:

  A a1(10); // ok 
A a2('x'); // error

B b;
A a3(b); //错误 - 假设(即使)b提供用户定义的转换为int

在线演示: a href =http://ideone.com/ZVyK7> http://ideone.com/ZVyK7



错误讯息非常清楚:


prog.cpp:9:5:错误:已删除函数'A :: A(char)'

prog.cpp:10:5:错误:已删除函数'A :: A(const B&)'



Suppose I have a class that I want to make sure my compiler (GCC in this case) doesn't synthesize any constructors or assignment methods for. I've found one way to do this which is to just include a const int member in the class, but this doesn't rub me to well. Is there an attribute or something which signifies this.

解决方案

If you define (or only declare) it yourself, then the compiler will not define it for you.

struct A
{
     A (); /*declaration is enough to prevent the compiler from 
             generating default constructor!*/
};

While the declaration is enough to prevent the compiler from generating default constructor, it is necessary to define it if your code requires the default constructor, otherwise you'll get linker error.

In C++11 (the new ISO Standard), you can disable constructors, copy-constructor, and copy-assignment as:

struct A
{
    A(const A&) = delete             //disable copy-constructor
    A& operator=(const A&) = delete; //disable copy-assignment
};


Now the interesting part

You can also selectively disable constructor(s) for selected types which makes delete more interesting. Consider this,

struct A
{
       A (int) {}
};

Object of this class can be created not only with int argument, but any type which implicitly converts to int. For example,

A a1(10);  //ok
A a2('x'); //ok - char can convert to int implicitly

B b; 
A a3(b); //ok - assume b provides user-defined conversion to int

Now suppose, for whatever reason, I don't want the users of class A to create objects with char or class B , which fortunately or unfortunately can implicitly convert to int, then you can disable them as:

struct A
{
     A(int) {}
     A(char) = delete;      //disable
     A(const B&) = delete;  //disable
};

Now here you go:

A a1(10);  //ok
A a2('x'); //error

B b; 
A a3(b); //error - assume (even if) b provides user-defined conversion to int

Online Demo : http://ideone.com/ZVyK7

The error messages are very clear:

prog.cpp:9:5: error: deleted function 'A::A(char)'
prog.cpp:10:5: error: deleted function 'A::A(const B&)'

这篇关于有没有办法禁用在类上的构造函数合成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆