Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: subtle template name conflict



Permlink Replies: 5 - Last Post: Jan 16, 2018 1:41 AM Last Post By: chen zero Threads: [ Previous | Next ]
chen zero

Posts: 30
Registered: 5/26/00
subtle template name conflict
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2018 5:24 AM
Hello,
I have a C++Builder XE code,
In short, I use Visitor pattern(a template class) to access some list data, however, I encounted a subtle problem, this is hard to describe clearly in post, please see the code, especially lines marked with ????

I upload the code on:
https://forums.embarcadero.com/thread.jspa?threadID=276246&stqc=true
Thank you very much !

Edited by: chen zero on Jan 13, 2018 5:44 AM

Edited by: chen zero on Jan 13, 2018 6:09 AM
chen zero

Posts: 30
Registered: 5/26/00
Re: subtle template name conflict
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2018 10:55 PM   in response to: chen zero in response to: chen zero
I found this issue might be related to "same name classes defined in different cpp files",
https://stackoverflow.com/questions/10671956/same-class-name-in-different-c-files
http://www.cplusplus.com/forum/general/32010/

It seems that the compiler behavior is not defined if duplicated name classes defined,
however, consider a big project, it might happen that there are duplicated name classes ....

Thanks!

Edited by: chen zero on Jan 13, 2018 11:29 PM
chen zero

Posts: 30
Registered: 5/26/00
Re: subtle template name conflict
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 15, 2018 3:17 AM   in response to: chen zero in response to: chen zero
Hello All,
I upload another minimized code: t2.zip,
https://forums.embarcadero.com/thread.jspa?threadID=276246&tstart=0

When hit the button on Form2, I think it should display msg: "LocalClassX in Unit2.cpp",
however, in my testing, it display: "LocalClassX in Unit1.cpp"

in my testing, almost all c++ compiler(gcc, vc) have this kind of undefind behavior.
consider a big project, whether compiler better give a warning of
"same name classes in different cpp files found" ?
Andy Walker

Posts: 72
Registered: 1/20/01
Re: subtle template name conflict
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 15, 2018 4:31 AM   in response to: chen zero in response to: chen zero
Hi Chen,

Just put your local class in it's own namespace and then you can access it via that namespace explicitly. See TForm2LocalClassX in code snippet;

namespace TForm2LocalClassX {
     class LocalClassX
     {
          public:
	String name;
	virtual void f() {
	     ShowMessage("LocalClassX in Unit2.cpp");
	}
     };
}
 
void __fastcall TForm2::Button1Click(TObject *Sender)
{
     TForm2LocalClassX::LocalClassX x;
     x.f();
}


Regards,

Andy

Edited by: Andy Walker on Jan 15, 2018 4:31 AM
chen zero

Posts: 30
Registered: 5/26/00
Re: subtle template name conflict
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 15, 2018 6:12 AM   in response to: Andy Walker in response to: Andy Walker
Hi Andy,
Thanks for trying the code.
but the problem root cause is: it's possible to define two classes with same name
in a namespace, or the default namespace.

It's valid to put LocalClassX(in Unit1.cpp) within namespace "TForm2LocalClassX", e.g:
// in Unit1.cpp, yes, in a small project, it's easy to find  that this class should not in 
// namespace:TForm2LocalClassX, but for a big project, it's not easy to find...
namespace TForm2LocalClassX {
class LocalClassX
{
public:
	String name;
	virtual void f() {
		ShowMessage("LocalClassX in Unit1.cpp: ");
	}
};
}


so, this is rely on
human to keep class unique within a namespace...

I think that compiler should give a warning of same named class defined in namspace
(or the default namespace), or link with the local defined class ....

Best Regards,

Edited by: chen zero on Jan 15, 2018 6:29 AM
chen zero

Posts: 30
Registered: 5/26/00
Re: subtle template name conflict
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 1:41 AM   in response to: chen zero in response to: chen zero
At last, the best solution for this local class name conflicts is:
using "anonymous namespace", I am very appreciated the guru in the gcc mailing list

e.g: in cpp files,
namespace {
class LocalClassX {
........
};
}

please note that in C++,
the default "empty namespace" is not same with "anonymous namespace"

A tip leart on these days is:
Always put local classes in cpp files in "anonymous namespace",
otherwise, it might happen undefined behavior.

Best Regards
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02