<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Feb 15, 2015 at 7:20 PM, Edward Diener <span dir="ltr"><<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@tropicsoft.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2/15/2015 9:19 PM, Richard Smith wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
On Sat, Feb 14, 2015 at 4:08 PM, Edward Diener<br>
<<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@tropicsoft.<u></u>com</a><br></span><span class="">
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>>> wrote:<br>
<br>
On 2/14/2015 5:48 PM, Reid Kleckner wrote:<br>
<br>
There might be a Clang bug here, depending on whether the copy<br>
ctor is<br>
supposed to be implicitly deleted or not. I forget if having a base<br>
class with a private copy ctor implicitly deletes derived class copy<br>
ctors or not.<br>
<br>
<br>
The second sentence above is not the case in my example, since there<br>
is no base class involved. I also did not compile in C++11 mode in<br>
my example AFAICS. Don't I need a command-line switch of -std=c++11<br>
for that ?<br>
<br>
<br>
In C++11, the copy constructor would be deleted because the base class<br>
dtor is inaccessible.<br>
</span></blockquote>
<br>
There is no base class in my example.</blockquote><div><br></div><div>Sorry, I was addressing Reid's question here. In your example, the copy constructor would be deleted because you have a non-copyable member, so the effect is the same.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In C++98, it will not be, because there is no<br>
notion of deleted functions.<br>
</blockquote>
<br></span>
Agreed. But I would expect in the case of my example that any compiler-defined copy constructor to be only a private declaration because the class is non-copyable. Either that or the compiler generates the standard copy constructor which copy constructs each member but does not flag it as an error because it is never actually called in the code.</blockquote><div><br></div><div>That's a possible approach. We'll need someone to experiment with mingw to discover what we should do to match it.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Clang's class dllexport semantics are modeled on MSVC's because they<br>
make more sense than GCC's for PE/COFF. As Nico said, the idea<br>
is that<br>
we emit all these special members (and inline functions!) so that<br>
clients can dllimport them and not have to emit definitions.<br>
This is an<br>
intentional semantic difference from ELF's visibility model,<br>
which has a<br>
different mechanism for ensuring that the addresses of inline<br>
functions<br>
are the same across DSO boundaries.<br>
<br>
If you can find a way to delete, explicitly or implicitly, the<br>
unwanted<br>
copy ctor, then Clang should skip it while exporting the rest.<br>
If not,<br>
that's a bug.<br>
<br>
<br>
Let me start by saying that both mingw/mingw64/gcc on Windows and<br>
VC++ compiles the example I gave without error using their<br>
respective Windows export keywords. Clang as I have shown does not.<br>
<br>
<br>
Presumably MSVC accepts because they build in C++11 mode (sort of --<br>
they don't *have* a separate C++98 mode). I would expect Clang to also<br>
accept the code that MSVC accepts when run in C++11 mode.<br>
</blockquote>
<br></span>
You are saying that in C++11 mode the code in my example should compile without error but in non-C++11 mode the code in my example should be in error.</blockquote><div><br></div><div>I was not talking about what should happen, I was talking about what I expect to happen, and why. Our behavior in MSVC + C++98 mode is not interesting, because there is no other compiler to be compatible with in that case, and as a result it's not really a mode that we care much about. Our behavior in either C++11 mode is probably fine. The interesting case is how we should behave in mingw + C++98 mode; it seems that's the one we get wrong, by deviating from mingw's behavior.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Whether or not clang generates an implicit copy constructor /<br>
copy-assignment operator when the user does not have one seems to me<br>
to be an irrelevant issue. If objects of the class are never<br>
copied/assigned why should clang generate an error ?<br>
<br>
<br>
Because, as has already been stated, marking a class as dllexport causes<br>
some of its implicitly-generated methods to be exported, which triggers<br>
their definition. We may be getting the "some" wrong here, or the rules<br>
for mingw and for MSVC might be different, but it *is* correct that we<br>
trigger the definition of some implicit members in some cases when the<br>
class is marked dllexport -- this is necessary for link compatibility<br>
with MSVC.<br>
</blockquote>
<br></span>
I am using the clang for Windows which targets mingw(64)/gcc not the one that targets MSVC.</blockquote><div><br></div><div>Right; I suspect we're getting the mingw compatibility wrong here, by assuming it's the same as MSVC (and it's possible that we get the MSVC compatibility wrong too, in the same way).</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This is regardless of whether the class is exported or not. I have a<br>
class that cannot be copied/assigned so naturally I see no need to<br>
create a user-defined copy constructor or copy-assignment operator.<br>
How can I be wrong not doing so ? Even when I export the class I am<br>
still telling users of the class that import it that it cannot be<br>
copied or assigned. That seems pretty clearcut to me. Clang now<br>
wants to tell me that if I export the class I must change how I code<br>
the class as to provide a user-defined copy constructor/assignment<br>
operator ( or a private declaration/deleted function depending on<br>
C++03/C++11 ) to satisfy clang ? That is not logically right from<br>
this end-user's point of view. I am also pretty sure that in C++03<br>
if a class is not copyable I am not required to provide a<br>
user-defined copy constructor or private copy constructor<br>
declaration when defining the class.<br>
<br>
Even in a C++11 compilation is it actually a C++ standard error if<br>
the programmer does not provide deleted versions of the copy<br>
constructor / assignment operator when objects of that class cannot<br>
be copied / assigned using the default generated copy constructor /<br>
assignment operator ? I do not think so. According to my reading of<br>
the C++11 standard as interpreted by Lippman in C++ Fifth Edition,<br>
<br>
"The synthesized copy constructor is defined as deleted if the class<br>
has a member whose own copy constructor is deleted or inaccesible..."<br>
<br>
"The synthesized copy-assignment operator is defined as deleted if a<br>
member has a deleted or inaccessible copy-assignment operator..."<br>
<br>
I realized I am not quoting from the C++11 standard but unless<br>
Lippman is in error here I cannot see other than that clang is in<br>
error in the case I have presented.<br>
<br>
<br>
You're using a language extension, so the guarantees of the C++ standard<br>
are not relevant.<br>
</blockquote>
<br></div></div>
You can make your own rule based on the fact that exporting a class is not mentioned in the C++ standard but I think your rule is wrong. You are telling programmers that they must supply a user-defined copy constructor for a class which is exported and is non-copyable, even when objects of the class are not meant to be copied and are not copied in code. Furthermore your previous comment implies that this must be done only in non-C++11 mode.</blockquote><div><br></div><div>For ABI compatibility, we are required to follow other compilers when we implement their dllexport attribute. We're not making up any rule here ourselves. If we're doing something different from what those other compilers do (as we evidently are), that is a bug. But it doesn't matter what the relevant language standards say, because this is an extension -- in the cases where we need to implicitly define a copy operation for ABI compatibility, we will do so, even if the source code doesn't otherwise "use" that function.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
I don't mind filing a bug report on this but I want to get some<br>
agreement here that clang is not doing the right thing even in the<br>
face of an "export" of the class. The job of defining an implicit<br>
copy constructor and copy-assignment operator is that of the<br>
compiler and if clang wants to claim that in the face of the<br>
"export" keyword it is not required to follow the standard C++ rules<br>
of how thiws should be done, I do not want to follow up with such a<br>
bug report.<br>
<br>
<br>
On Fri, Feb 13, 2015 at 11:26 PM, Edward Diener<br></span><span class="">
<eldlistmailingz@tropicsoft.__<u></u>com<br>
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>__<a href="http://tropicsoft.com" target="_blank">trop<u></u>icsoft.com</a><br>
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>>>><br>
wrote:<br>
<br>
On 2/14/2015 1:22 AM, Nico Weber wrote:<br>
<br>
On Fri, Feb 13, 2015 at 8:43 PM, Edward Diener<br></span><span class="">
<eldlistmailingz@tropicsoft.__<u></u>__com<br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>__<a href="http://tropicsoft.com" target="_blank">trop<u></u>icsoft.com</a><br>
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>>><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>>__<a href="http://trop__icsoft.com" target="_blank">tro<u></u>p__icsoft.com</a> <<a href="http://tropicsoft.com" target="_blank">http://tropicsoft.com</a>><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>__<a href="http://tropicsoft.com" target="_blank">trop<u></u>icsoft.com</a><br>
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>>>>><br>
wrote:<br>
<br></span><div><div class="h5">
On 2/13/2015 11:17 PM, Nico Weber wrote:<br>
<br>
Hans would know for sure, but I think this is<br>
intentional: Since<br>
AClass<br>
is dllexported, all its implicit functions get<br>
generated (so<br>
that they<br>
can be exported from the dll), which means<br>
they need to be<br>
semantically<br>
checked. If you give the class a deleted copy<br>
constructor and<br>
assignment<br>
operator, it might work.<br>
<br>
<br>
Where in the C++ standard is such behavior justified ?<br>
<br>
<br>
The C++ standard doesn't talk about dllexport, so it<br>
won't say<br>
either way.<br>
<br>
This isn't a question of standards, it's a question of<br>
practicality. The<br>
way dllexport / dllimport are used is usually like so<br>
(think of<br>
a class<br>
that has a valid copy constructor for a bit):<br>
<br>
#ifdef IMPL<br>
#define EXPORT __attribute__((dllexport))<br>
#else<br>
#define EXPORT __attribute((dllimport))<br>
#endif<br>
<br>
class EXPORT MyClass { ... };<br>
<br>
Then, while building your dll, you define IMPL, and<br>
while building<br>
users, you don't.<br>
<br>
<br>
I know this of course.<br>
<br>
When the dll is being built, the compiler doesn't know<br>
if clients are going to use MyClass's implicit copy<br>
constructor,<br>
so its<br>
choices are:<br>
<br>
1.) Generate it when it's dllexported and put it into<br>
the dll.<br>
Then, if<br>
clients use it, they'll get it from the dll.<br>
2.) Don't generate it when it's dllexported.<br>
2a) Don't even generate the implementation when clients<br>
use it.<br>
Now the<br>
copy constructor isn't defined anywhere and you'll get<br>
linker<br>
errors.<br>
2b) Emit a weak definition into every TU that uses it.<br>
I think this<br>
would work, but you end up with several copy<br>
constructors – if you<br>
wanted this, you probably wouldn't have EXPORTed the class.<br>
<br>
Because of this reasoning (I would guess), clang<br>
chooses 1 and<br>
defines<br>
all implicit methods for dllexported classes. Now, in<br>
your case the<br>
definition of some of these implicit methods causes an<br>
error. I<br>
suppose<br>
clang's logic could instead be "only define all<br>
implicit methods<br>
that<br>
are valid", but I think this can be different in<br>
different TUs.<br>
Consider:<br>
<br>
class A;<br>
class EXPORT B { unique_ptr<A> a_; };<br>
<br>
I think the implicit destructor of B will be valid in<br>
TUs that<br>
happen to<br>
include the header that defines A but not in others. So<br>
that doesn't<br>
seem like a good rule.<br>
<br>
<br>
A TU that does not include the header that defines A is<br>
just being<br>
stupid so I think the compiler can choose to assume that an<br>
implicit<br>
destructor can be generated. And if there really is no<br>
definition<br>
for A then the design of B is just an error. The compiler<br>
should<br>
make reasonable choices. For B since C++11 is necesary for<br>
unique_ptr, deleted copy constructors and assignment operators<br>
should be generated if the compiler does not encounter any<br>
since<br>
unique_ptr is not copyable or assignable.<br>
<br>
The problem is that you are forcing class designers, when a<br>
class is<br>
exportable, to generate copy constructors and assignment<br>
operators<br>
even when objects of that class are never meant to be copied or<br>
assigned. Of course the practical solution for this is to<br>
generate<br>
private declarations in C++03 and deleted declarations in<br>
C++11, but<br>
still it seems silly to enforce this. Instead the compiler<br>
should<br>
automatically generate these solutions when it sees that a<br>
class is<br>
not copyable or when it sees that a class is not<br>
assignable, and the<br>
designer has not provided his own declarations.<br>
<br>
I really think it is wrong for clang to force class<br>
designers, even<br>
when exporting a class, to do something he would ordinarily<br>
never<br>
have to do in C++.<br>
<br>
BTW gcc works correctly in the same situation in not<br>
generating any<br>
error when '__attribute__((dllexport))' is being used for<br>
mingw/gcc,<br>
if that means anything to clang.<br>
<br>
Also VC++ works correctly in the same situation when<br>
__declspec(dllexport) is being used, if that means anything<br>
to clang.<br>
<br>
Also as I noticed, when<br></div></div>
'__attribute__((__visibility__<u></u>______("default")))' is being<span class=""><br>
used,<br>
which I assume is the Linux equivalent of 'exporting' a<br>
class, clang<br>
evidently does the right thing and generates no error. Why<br>
does it<br>
therefore operate differently with<br>
'__attribute__((dllexport))' ?<br>
<br>
Is it a valid Boost solution to use<br></span>
'__attribute__((__visibility__<u></u>______("default")))' for clang on<span class=""><br>
Windows targeting mingw/gcc as a workaround for this<br>
problem instead<br>
of having to change the exported class itself ? This of<br>
course would<br>
not solve the problem of using clang on Windows targeting VC++,<br>
where I assume '__attribute__((dllexport))' is correct and<br></span>
'__attribute__((__visibility__<u></u>______("default")))' would not be<span class=""><br>
recognized, but I am less interested in that compiler for<br>
Boost.<br>
<br>
<br>
Nico<br>
<br>
<br>
The C++ standard does tell us I believe that if an<br>
object<br>
of a class<br>
is not copied there is never a need to specify a copy<br>
constructor<br>
and if a class is not assigned there is never a<br>
need to<br>
specify an<br>
assignment operator, whether or not one has a<br>
non-copyable or<br>
non-assignable member or not. Changing that basic<br>
rule to<br>
support<br>
something in clang related to "exported' classes<br>
cannot be<br>
correct IMO.<br>
<br>
Why does<br></span>
'__attribute__((__visibility__<u></u>______("default")))'<span class=""><br>
work but<br>
'__attribute__((dllexport))' does not ? Aren't<br>
they both the<br>
equivalent of "exporting" a class in clang ?<br>
<br>
I realize that for clang on Windows targeting<br>
mingw/gcc and<br>
not VC++<br>
that the correct "export" attribute is probably<br></span>
'__attribute__((__visibility__<u></u>______("default")))'<span class=""><br>
and not<br>
'__attribute__((dllexport))'. But I heavily object<br>
to the<br>
idea that<br>
because I might be exporting a class the rules of<br>
copyability or<br>
assignability for that class must change contrary<br>
to the C++<br>
standard. This also puts an extra conceptual<br>
burden on the<br>
design of<br>
a class.<br>
<br>
<br>
On Fri, Feb 13, 2015 at 8:07 PM, Edward Diener<br></span>
<eldlistmailingz@tropicsoft.__<u></u>____com<span class=""><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>>__<a href="http://trop__icsoft.com" target="_blank">tro<u></u>p__icsoft.com</a> <<a href="http://tropicsoft.com" target="_blank">http://tropicsoft.com</a>><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>__<a href="http://tropicsoft.com" target="_blank">trop<u></u>icsoft.com</a><br>
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>>>><br></span>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a> <mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>>>__<a href="http://tro__p__icsoft.com" target="_blank">tr<u></u>o__p__icsoft.com</a><br>
<<a href="http://trop__icsoft.com" target="_blank">http://trop__icsoft.com</a>> <<a href="http://tropicsoft.com" target="_blank">http://tropicsoft.com</a>><span class=""><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>>__<a href="http://trop__icsoft.com" target="_blank">tro<u></u>p__icsoft.com</a> <<a href="http://tropicsoft.com" target="_blank">http://tropicsoft.com</a>><br>
<mailto:<a href="mailto:eldlistmailingz@" target="_blank">eldlistmailingz@</a>__<a href="http://tropicsoft.com" target="_blank">trop<u></u>icsoft.com</a><br>
<mailto:<a href="mailto:eldlistmailingz@tropicsoft.com" target="_blank">eldlistmailingz@<u></u>tropicsoft.com</a>>>>>><br>
wrote:<br>
<br>
I am compiling code with "-c -x c++ -O0<br>
-g -fno-inline<br>
-Wall -g"<br>
using clang targeting mingw/gcc on Windows.<br>
<br>
This code compiles with no errors (<br>
boost::scoped_ptr<T> is<br>
non-copyable, non-assignable ):<br>
<br>
#include <boost/scoped_ptr.hpp><br>
class<br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
int main() { return 0; }<br>
<br>
This code compiles with no errors:<br>
<br>
#include <boost/scoped_ptr.hpp><br>
class<br></span>
__attribute__((__visibility__(<u></u>________"default")))<span class=""><br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
int main() { return 0; }<br>
<br>
but this code compiles with errors:<br>
<br>
#include <boost/scoped_ptr.hpp><br>
class __attribute__((dllexport))<br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
int main() { return 0; }<br>
<br>
test_clang_bug.cpp:3:1: error: field<br>
of type<br>
'boost::scoped_ptr<int>' has private copy<br>
constructor<br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
^<br>
<br></span>
..\..\..\boost/smart_ptr/_____<u></u>___scoped_ptr.hpp:47:5:<span class=""><br>
note:<br>
declared<br>
private here<br>
scoped_ptr(scoped_ptr const &);<br>
^<br>
test_clang_bug.cpp:3:1: note:<br>
implicit copy<br>
constructor for<br>
'AClass' first required here<br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
^<br>
test_clang_bug.cpp:3:1: error:<br>
'operator=' is<br>
a private<br>
member<br>
of 'boost::scoped_ptr<int>'<br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
^<br>
<br></span>
..\..\..\boost/smart_ptr/_____<u></u>___scoped_ptr.hpp:48:18:<div><div class="h5"><br>
<br>
note: declared<br>
private here<br>
scoped_ptr &<br>
operator=(scoped_ptr const &);<br>
^<br>
test_clang_bug.cpp:3:1: note:<br>
implicit copy<br>
assignment<br>
operator<br>
for 'AClass' first required here<br>
AClass { boost::scoped_ptr<int><br>
sp_pointer; };<br>
^<br>
2 errors generated.<br>
<br>
<br>
Comments ?<br>
<br>
Should I file a bug report ?<br>
<br>
I do not see why, even "exporting" a<br>
class, clang<br>
should<br>
give an<br>
error. If I am not copying/assigning an<br>
instance<br>
of a class<br>
I should<br>
never get a compiler error telling me<br>
that some<br>
member is not<br>
copyable or assignable.<br>
</div></div></blockquote><div class="HOEnZb"><div class="h5">
<br>
<br>
<br>
______________________________<u></u>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/cfe-dev</a><br>
</div></div></blockquote></div><br></div></div>