What's the problem on this cpp code?
Notice! This post is disaster. meaningless. just waste your time. :(
I’ve warn you.
Trying to dig the error on following c++ code.
#include <iostream>
struct NoImplicitCopy {
NoImplicitCopy() = default;
explicit NoImplicitCopy(const NoImplicitCopy& ) = default;
};
NoImplicitCopy foo()
{
NoImplicitCopy n;
return n;
}
void bar(NoImplicitCopy n)
{ }
int main(void)
{
NoImplicitCopy n;
NoImplicitCopy x(n);
n = foo();
bar(n);
return EXIT_SUCCESS;
}
from Explicit copy-constructor
When I compile above code, I got following error message. and I don’t understand why the error occurred.
Even if I read the Explicit copy-constructor
web page. :(
What a dumb head and dumber my intelligence.
Anyway… the error message is like followings.
[enjian@localhost build]$ g++ main.cpp
main.cpp: In function ‘NoImplicitCopy foo()’:
main.cpp:11:9: error: no matching function for call to ‘NoImplicitCopy::NoImplicitCopy(NoImplicitCopy&)’
11 | return n;
| ^
main.cpp:4:2: note: candidate: ‘constexpr NoImplicitCopy::NoImplicitCopy()’
4 | NoImplicitCopy() = default;
| ^~~~~~~~~~~~~~
main.cpp:4:2: note: candidate expects 0 arguments, 1 provided
main.cpp: In function ‘int main()’:
main.cpp:23:7: error: no matching function for call to ‘NoImplicitCopy::NoImplicitCopy(NoImplicitCopy&)’
23 | bar(n);
| ^
main.cpp:4:2: note: candidate: ‘constexpr NoImplicitCopy::NoImplicitCopy()’
4 | NoImplicitCopy() = default;
| ^~~~~~~~~~~~~~
main.cpp:4:2: note: candidate expects 0 arguments, 1 provided
main.cpp:14:25: note: initializing argument 1 of ‘void bar(NoImplicitCopy)’
14 | void bar(NoImplicitCopy n)
| ~~~~~~~~~~~~~~~^
[enjian@localhost build]$
Let’s find out what is the problem.
…few days later…
Figure out the source of error message. But still don’t know the reason. :(
To compile, just remove explicit
keyword. but why? why?? why???
struct NoImplicitCopy {
NoImplicitCopy() = default;
- explicit NoImplicitCopy(const NoImplicitCopy& ) = default;
+ NoImplicitCopy(NoImplicitCopy&) = default;
};
The result is.
[enjian@localhost build]$ g++ main.cpp
[enjian@localhost build]$
Let’s found out what the explicit
keyword doing.
And, I found the similar questions on stackoverflow.\
There are very important quote.\
Foo copy = original
. The same happens when passing/returning to/from a function…
The explicit copy constructor means that the copy constructor will not be called implicitly, which is what happens in the expression:
CustomString s = CustomString("test")
This expression literally means: create a temporaryCustomString
using the constructor that takes aconst char*
. Implicitly call the copy constructor ofCustomString
to copy from that temporary infos
.
You can call the copy constructor explicitly:
CustomString s(CustomString("test"))
In, cpprefernce has document about explicit specifier.
And it saids as follows (and has example code).
- Specifies that a constructor or conversion frunction is explicit, that is, it cannot be used for implicit conversion and copy-initialization.
What The…
In the copy initialization has advert about above sentences.
If T is a class type and the cv-unqualified version of the type of other is T or a class derived from T, the non-explicit constructiors of T are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
…Anyway. The conclusion is ‘Do NOT append explicit
specifier where use copy constructor.’
And that’s the SPEC!