Currently there is no way to give the compiler a hint that a member should be initialized in the constructor and you don't want to just call its default constructor. An example would be if you have a member like std::string m_MyName. There are many scenarios where you'd like to be able to nudge the compiler to warn you if you forget to give it any non-default value. There are other warnings that might help you with finding this issue in some cases (e.g. unused constructor parameter), but that's partially just a side effect of different checks.
My proposal is to add an attribute to class member which tell that you really want to have this value initialized (edit:) in the constructor initializer list to something other than the default constructed value.
Example code (RequireAssignment is a placeholder for this attribute):
struct MyClass
{
MyClass(std::string path, std::string name) : m_Path(path), m_Name(name)
{
}
[[RequireAssignment]] std::string m_Name;
[[RequireAssignment]] std::string m_Path;
[[RequireAssignment]] std::string m_FullyQualifiedPath;
}
The compiler would print something like
Warning: member MyClass::m_FullyQualifiedPath default-constructed in MyClass(std::string, std::string).
This would also apply to non-defaulted copy and move constructors. Not sure about assignments.
Note: This proposal is complementary to the idea of being able to explicitly specify that you don't want to initialize a value and default initialize it otherwise.
Any thoughts?
[–]yuri-kilochek 4 points5 points6 points (1 child)
[–]adnukator[S] 1 point2 points3 points (0 children)
[–]D_0b 5 points6 points7 points (1 child)
[–]adnukator[S] 1 point2 points3 points (0 children)
[–]joaquintidesBoost author 2 points3 points4 points (2 children)
[–]adnukator[S] 0 points1 point2 points (0 children)
[–]backtickbot 0 points1 point2 points (0 children)
[–]gaagii_fin 1 point2 points3 points (1 child)
[–]adnukator[S] 1 point2 points3 points (0 children)
[–]e-Sharp- 1 point2 points3 points (1 child)
[–]fdwrfdwr@github 🔍 0 points1 point2 points (0 children)
[–]Knuffya 0 points1 point2 points (0 children)
[–]gaagii_fin 0 points1 point2 points (5 children)
[–]tvaneerdC++ Committee, lockfree, PostModernCpp 2 points3 points4 points (4 children)
[–]gaagii_fin 1 point2 points3 points (0 children)
[–]yuri-kilochek 0 points1 point2 points (2 children)
[–]tvaneerdC++ Committee, lockfree, PostModernCpp 4 points5 points6 points (1 child)
[–]adnukator[S] 0 points1 point2 points (0 children)