Hi,

warnings
========

under Windows there exist the following four warnings:

C4625: copy constructor could not be generated because a base class copy constructor is inaccessible C4626: assignment operator could not be generated because a base class assignment operator is inaccessible
C4511: copy constructor could not be generated
C4512: assignment operator could not be generated


These warnings occur when a base class is not copyable or a member variable is not copyable which causes the class itself to be non copyable as well.

C4625 and C4626 occur in a direct derivation of a non-copyable class,
C4511 and C4512 in second level or further derivations.

The warning gives only redundant informaton, because every time one tries to copy (or assign) such a class, the compiler will throw an error.


Example code
============
class Abc
{
    public:
        Abc();
    private:
        // Idiom to make a class non-copyable:
        Abc(const Abc&);          // not accessible
        Abc & operator=(const Abc&);  // ... as well
};

class Bcd
{
  public:
        Bcd(int a) : n(a) {}

    private:
        int n;
        Abc x;  // Non copyable member
};      // Declaration of class Bcd causes warnings C4625  and C4626

class C : public Bcd    // non copyable base class
{public:
    C() {}
};      // Declaration of class C causes warnings C4511 and C4512

void f()
{

    Bcd a(5);
    Bcd b(a);   // error
    a = b;      // error

    C c;
    C d(c);     // error
    c = d;      // error
}


workaround when keeping the warnings
====================================

The warnings can be work-arounded by declaring copy-constructor and assignment-operator explicitely private in every derived or containing class like this:

class C : public Bcd
{public:
    C() {}
 private:
    // Avoid copying:
    C(const C&);    // declared but not implemented
    C & operator=(const C&);    // ...
};      

This is a possible workaround which ...

positively: makes it immediately visible that this class is not to copy

negatively1: annihilates the boost idiom of deriving from class noncopyable
(see http://www.boost.org/libs/utility/utility.htm#Class_noncopyable)

negatively2: is much more writing work and not clearer than the boost solution - see here:

class C : public boost::noncopyable
    C() {}
};      



Conclusion and suggestion
=========================

1)
The warnings don't give any additional information. Any attempt to copy such a class results in a compiler error anyway.

2)
The warnings annihilate the boost noncopyable-idiom

3)
The warning tells the developer something that was intended anyway, so he does not want to be told again.
In case it was not intended, see 1)


So, my suggestion: let's switch off the windows warnings
C4625, C4626, C4511, C4512.


Regards,
Nikolai

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to