Re: How = delete would work ?
On 11 Feb., 09:58, gute...@gmail.com wrote:
On 10 ???, 23:27, SG <s.gesem...@gmail.com> wrote:
This example compiles using g++ 3.4.5:
class A {
A(A&);
public:
A() {}
template<typename T>
A(T const&) {}
};
void f(A a);
void test() {
const A a;
f(a);
}
MSVC sees an ambiguity here, the same for example by Richard. Is this
just another MSVC bug ?
I don't see why there would be any ambiguity. A::A(A&) is selected for
non-const lvalues because the other constructor is less attrative.
It's less attractive because it's a template and/or it requires a
qualifier conversion. For rvalues or const lvalues the an instance of
the templated constructor is the only candidate in the overload
resolution set.
Anyway, I'm still confused. Assuming a class with deleted copy
constructor with non-const refererence and template unary constructor,
taking const reference to template parameter class, like in original
example. Which is true:
1. The class can be copied if it has constness, so the behaviour of "=
delete" is the same as having a special function declared and not
defined (as "Triple-DES" noted).
What do you mean by "it it has constness"?
It can't be copied from non-const lvalues.
The "=delete" syntax is a more elegant way of saying that this
function is not allowed to be called compared to making it private and
providing no implementation. The only difference is that inside this
class (or a friend of this class) this constructor can be invoked. So,
the private and declared-only constructor can delay a coding error
until link time.
2. The class can always be copied, = delete would make the copy
constructor not exist, so there's no other choice than template
constructor. So, "= delete" brings something new to this.
No. A deleted function is like a real function in terms of overload
resolution. If overload resolution picks a deleted function the
program is ill-formed and the compiler will reject the code. So, you
could use "=delete" for explicitly disabling an implicit conversion:
void foo(double);
void foo(int) = delete;
int test() {
foo(42); // ill-formed
}
3. Can this be used to make constructor with more than one arguments
act as copy constructor ? example:
struct B
{
B() {}
B(B&) = delete;
B(B const&, int = 0) {}
};
You don't need a deleted copy constructor for this. Your example makes
B non-copyable when the source is a non-const lvalue. B(B const&, int
= 0) already is a copy constructor that prevents the compiler from
generating another default copy constructor. Otherwise you would have
an ambiguity.
Cheers!
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]