如何仅针对非常量对象定义一些构造函数?
How to define some constructors as for not-constant objects only?
关注 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
永远不会回到原来的状态空 (std::nullopt
)
我的问题是,如果这些对象被标记为 [=20,我能否以某种方式告诉编译器永远不允许使用第一个构造函数(默认构造函数:Foo()
)构造类型为 Foo
的对象=]
这样就没有机会出现这种类型的代码了:
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
的信息(在对象 class 构造代码或定义中),以便我可以使用 SFINAE 或其他工具来拆分,区分 const
和 not-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
对象不能先调用它 "method",但事实并非如此允许我知道并检查到这一点。
(类型 Bar
不能设置为某种默认值,空值。它要么是,要么不是,我想保持这种状态)
我也不知道为什么不能用 const
标记构造函数,因为那样(在我看来)所有那些 classes 对象都可以部分构建,但之后仍然必须完全实现才能正常工作或发挥其潜力。
我找不到任何其他有同样问题的程序员所以我提出了扩展代码示例来检查它是否真的是一个问题(使用不同的方法来创建 const
对象)或者我只是简单地做事情以错误的方式。是否有任何我遗漏的设计模式、编程风格等导致这些问题不存在?
我知道在 run-time
const
和 not-const
对象之间没有区别,但这不是重点。我相信这个问题可以在 compile-time
得到解决,我只是不知道如何告诉编译器我的想法。
编辑:
使用 Foo()
创建对象很有用(仅使用其部分功能),但前提是稍后它将完成需要 m_data
.
的主要工作
不,在构造函数中不可能知道对象是常量还是非常量。因此,无法实现您想要的功能。您有两个选择:
- 删除默认构造函数。如果
doJob
是默认构造对象的错误,那么为什么不禁止这种无效状态?
- 接受
const Foo c;
没用的事实,但编译器不会告诉程序员。如果程序员测试他们编写的代码,他们应该很快就会发现这一点。 class. 的文档中的无用性也应该变得非常清楚
关注 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
永远不会回到原来的状态空 (std::nullopt
)
我的问题是,如果这些对象被标记为 [=20,我能否以某种方式告诉编译器永远不允许使用第一个构造函数(默认构造函数:Foo()
)构造类型为 Foo
的对象=]
这样就没有机会出现这种类型的代码了:
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
的信息(在对象 class 构造代码或定义中),以便我可以使用 SFINAE 或其他工具来拆分,区分 const
和 not-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
对象不能先调用它 "method",但事实并非如此允许我知道并检查到这一点。
(类型 Bar
不能设置为某种默认值,空值。它要么是,要么不是,我想保持这种状态)
我也不知道为什么不能用 const
标记构造函数,因为那样(在我看来)所有那些 classes 对象都可以部分构建,但之后仍然必须完全实现才能正常工作或发挥其潜力。
我找不到任何其他有同样问题的程序员所以我提出了扩展代码示例来检查它是否真的是一个问题(使用不同的方法来创建 const
对象)或者我只是简单地做事情以错误的方式。是否有任何我遗漏的设计模式、编程风格等导致这些问题不存在?
我知道在 run-time
const
和 not-const
对象之间没有区别,但这不是重点。我相信这个问题可以在 compile-time
得到解决,我只是不知道如何告诉编译器我的想法。
编辑:
使用 Foo()
创建对象很有用(仅使用其部分功能),但前提是稍后它将完成需要 m_data
.
不,在构造函数中不可能知道对象是常量还是非常量。因此,无法实现您想要的功能。您有两个选择:
- 删除默认构造函数。如果
doJob
是默认构造对象的错误,那么为什么不禁止这种无效状态? - 接受
const Foo c;
没用的事实,但编译器不会告诉程序员。如果程序员测试他们编写的代码,他们应该很快就会发现这一点。 class. 的文档中的无用性也应该变得非常清楚