2011/11/9 Jeff Walden <
jwald...@mit.edu>:
> If you find yourself reaching for the private-but-not-implemented idiom to
> prevent copy-construction and default assignment in classes you write --
> e.g. this:
>
> struct S
> {
> private:
> S(const S& other);
> void operator=(const S& other);
> };
>
> Please also use the new MOZ_DELETE macro in mozilla/Types.h when you do so:
>
> struct S
> {
> private:
> S(const S& other) MOZ_DELETE;
> void operator=(const S& other) MOZ_DELETE;
> };
>
> This macro encapsulates C++11 deleted function syntax, which causes any use
> of the corresponding function to be a compile-time (not link-time) error.
I don't get this: why would that be a link-time error? If a compiler
doesn't generate a compile-time error already for the first form, it's
buggy. I checked with g++ 4.6.1:
This program:
struct S
{
S() {}
private:
S(const S& other);
void operator=(const S& other);
};
int main()
{
S a;
S b(a);
S c;
c = a;
}
Gives me these compile errors with G++ 4.6.1:
$ g++ a.cpp -o a
a.cpp: In function ‘int main()’:
a.cpp:5:5: error: ‘S::S(const S&)’ is private
a.cpp:12:9: error: within this context
a.cpp:6:10: error: ‘void S::operator=(const S&)’ is private
a.cpp:14:8: error: within this context
Benoit