I have a functor whose declaration looks like this:
class logger {
public:
log_t operator() (unsigned _LogLevel) {
return log_t{_LogLevel};
}
};
As in the code, I want this functor to construct and return a log_t object. But the compiler complains that copy constructor of the log_t object has been deleted.
I admit, class type log_t has no copy constructor, only a move constructor and an explicit ordinary constructor with one parameter. Due to log_t is derived from the standard library template class basic_ostream, and the copy constructor of this class has been explicitly deleted, so I can only provides a move constructor to log_t objects.
In addition, this object cannot use static to return a reference due to its lifecycle requirements.
I have added -std=c++11
in the compilation options. In my opinion, regardless of whether the RVO option is turned on or not, according to the C++11 standard, the function return value should give priority to calling the move constructor rather than the copy constructor. In this imitation function, the copy constructor should not be needed, but it actually reports an error.
I've tried this following answer but I failed: "To make use of this feature of C++11, the constructor (taking int in this case) has to be non-explicit though." ( Can we return objects having a deleted/private copy/move constructor by value from a function? )
What is the reason for this error? How can I solve this problem?
Aucun commentaire:
Enregistrer un commentaire