cppguy
cppguy

Reputation: 3713

Is there such a thing as a const constructor?

Today I accidentally put const at the beginning of my constructor (copy paste mistake) and it compiled. I've only tried this in Visual Studio 2008 which is pre C++11. Does this syntax have any meaning? Is this Microsoft's early attempts at constexpr?

class foo
{
public:
    const foo(int i){}
};

foo f(1);

Upvotes: 6

Views: 121

Answers (1)

vsoftco
vsoftco

Reputation: 56567

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

Although I am not using it, MSVS is not the best compiler in terms of standard-compliance, at least that's what I realized from various questions on this site.

Upvotes: 4

Related Questions