Is there such a thing as a const constructor?

Today I accidentally put const at the beginning of my constructor (error copying folders) and compiled it. I just tried this in Visual Studio 2008, which is pre C ++ 11. Does this syntax make any sense? Is this an early Microsoft attempt at constexpr ?

 class foo { public: const foo(int i){} }; foo f(1); 
+6
source share
1 answer

Your code is not standard, there is no such thing. However, starting with C ++ 11, you can have constexpr constructors, so your object is created at compile time and can also be used in constexpr expressions.

Although I do not use it, MSVS is not the best compiler in terms of standard compliance, at least for what I understood from various questions on this site.

+4
source

Source: https://habr.com/ru/post/984662/


All Articles