|
-
December 11th, 2008, 11:04 AM
#6
Re: VC++ 10 impact on existing code?
 Originally Posted by joncaves
There is one change that fell out of the implementaion of rvalue references that has broken some code bases internally. It is code like the following:
Code:
struct S
{
S();
explicit S(const S&);
};
void f(S);
int main()
{
S s;
f(s);
}
This code compiles with Visual C++ 2008 SP1 (and with previous compilers) but it is, correctly, rejected by Dev10. If you make the default copy-constructor of a class 'explicit' you are saying that you can't pass an instance of the class to a function. The "fix" in every case we have seen so far is to remove the 'explicit' keyword from the default copy-constructor - it was usually added as a result of over enthusiasm.
Learnt something new. I never understood why people make copy constructors explicit because as far as I knew it only prevented assignment-looking calls to the copy constructor.
ie.
classname a;
classname b(a); // is ok if copy constructor is explicit
classname c = a; // fails if classname has explicit copy constructor.
I suppose thinking about it passing by value would be an implicit call and not an explicit call.
Is there ever a good reason to make copy constructors explicit?
Personally i have only used explicit constructors for single argument or multiargument constructors where all but one argument has a default value.
While talking about explicit, is it so hard to implement explicit conversion operators which is something I felt c++ needed for years?
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|