c++ - 如何仅针对非常量对象定义一些构造函数?

标签 c++ constants compile-time

有以下类(class):

class Foo {
public:
  Foo() : m_data{std::nullopt} {} // this is fine (not having m_data set), 
                                  // but only at the beginnig of objects lifetime
  Foo(Bar data) : m_data{data} {}

  void setData(Bar newData);
  Bar getData() const;

  void doJob() const; // requires m_data to be set (not std::nullopt)
  void doOtherJob() const; // does not require m_data to be set.
  /*...*/

private:
  std::optional<Bar> m_data;

一开始,m_data 可能未设置,但仅限于第一次 setData() 调用,然后是 m_data code> 永远不会变回空 (std::nullopt)

我的问题是,我能否以某种方式告诉编译器永远不允许使用第一个构造函数(默认构造函数:Foo())构造 Foo 类型的对象(如果这些对象)被标记为 const 这样就不会出现这种类型的代码:

const Foo x;           // without const totally fine
// x.setData( Bar{} ); // but with it, setData cannot be called
x.doJob();             // which makes that call a 100% throw error
                       // and in general, whole object is less usefull now
                       // because it is constructed only partially, 
                       // and cannot be finished 

要通过编译阶段吗?

最终效果应该是这样的:

Bar data = getSomeData();

Foo a;             // fine
Foo b{data};       // fine
const Foo c;       // error, this type will never be usefull / is artificially limited, 
                   // no logical reason to make it const
const Foo d{data}; // fine

有没有办法在编译时获取有关给定新对象(在对象类构造代码或定义中)的 const'ness 的信息,以便我可以使用 SFINAE 或其他东西来拆分,区分 constnot-const 对象的不同构造例程?

我希望它在编译时完成。

如果能够写出类似这样的内容那就太好了:

class Foo{
public:
                                    // both can still mutate everything
  Foo() { /*...*/ }                 // but this one cant be called by const objects            
  Foo(Bar data) const { /*...*/ }   // and this one can
};

它不会尝试创建 this 指针 const Foo* 而只是假装它,所以 const 对象无法首先调用它”方法”,但据我所知并检查过,这是不允许的。

(类型Bar不能设置为某种默认值,空值。它要么是,要么不是,我想保持这种状态)

我也不知道为什么不能用 const 标记构造函数,因为这样(在我看来)所有那些可以部分构造对象的类就不会有问题,但之后仍然必须完全履行才能正常工作或发挥其潜力。

我找不到任何其他有同样问题的程序员,所以我提出了扩展代码示例来检查这是否真的是一个问题(有不同的方法来创建 const 对象)或者我只是简单地这样做以错误的方式做事。是否有任何我遗漏的设计模式、编程风格等使得这些问题不存在?

我知道在运行时constnot-const 对象之间没有区别,但这不是重点。我相信这个问题可以在编译时解决,我只是不知道如何告诉编译器我的想法。

编辑: 使用 Foo() 创建对象很有用(仅使用其部分功能),但前提是稍后它将完成需要 m_data 的主要工作。

最佳答案

不,在构造函数中不可能知道该对象是 const 还是非常量。因此,您想要的功能是不可能实现的。您有两个选择:

  • 删除默认构造函数。如果 doJob 对于默认构造对象来说是一个错误,那么为什么不禁止这种无效状态呢?
  • 接受 const Foo c; 没有用的事实,但编译器不会告诉程序员。如果程序员测试他们编写的代码,他们应该很快就会发现这一点。从类的文档中,这种无用性也应该变得非常清楚。

关于c++ - 如何仅针对非常量对象定义一些构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163948/

相关文章:

c - char s[] 和 char *s 有什么区别?

c++ - 在 const 方法中修改非常量对象的非可变成员是未定义行为吗?

c# - 我找到了一个依赖于在其扩展中定义的方法的类。这是一种模式吗?

regex - 在 D 中的编译时评估正则表达式

c++:覆盖父类中的模板成员函数

c++ - 错误 : Expression must have class type?

c - 为什么在 C 语言中不经常使用 const 指针

c++ - 如何打开参数包包装器?

c++ - 如何在 OpenGL 3.2 中绘制全屏四边形?

c++ - 无法弄清楚如何在 CRTP 模板类上正确地专门化 fmt 模板