I expected that it does not compile using -fno-elide-constructors flag,
The given flag has no effect on return value optimization(aka RVO) from C++17 and onwards. This is because it is not considered an optimization anymore(from C++17) but instead it is a language guarantee.
From mandatory copy elison:
Under the following circumstances, the compilers are required to omit the copy and move construction of class objects, even if the copy/move constructor and the destructor have observable side-effects. The objects are constructed directly into the storage where they would otherwise be copied/moved to. The copy/move constructors need not be present or accessible:
- In the initialization of an object, when the initializer expression is a prvalue of the same class type (ignoring cv-qualification) as the variable type:
(emphasis mine)
This means that t_int
is constructed directly from the prvalue
1
. And the flag -fno-elide-constructors
has no effect on RVO which is different from NRVO. And so the copy constructor being deleted has no effect in your case.
Perhaps an example might help illustrating the same,
struct Custom
{
public:
Custom(int p): m_p(p)
{
std::cout<<"converting ctor called"<<std::endl;
}
Custom(const Custom&) = delete; //deleted copy ctor
private:
int m_p = 0;
};
int main()
{
Custom c = 1; //works in C++17 but not in Pre-C++17
}