Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!Xl.tags.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 13 Sep 2015 08:00:02 -0500 Return-Path: Sender: std-cpp-request@vandevoorde.com Approved: james.dennett@gmail.com Message-ID: Newsgroups: comp.std.c++ From: Bo Persson Subject: Re: Template argument name resolution and compiler discrepancies Organization: unknown References: <45eda1c8-7f97-40eb-9877-709c5c7585a2@googlegroups.com> Content-Type: text/plain; charset=windows-1252; format=flowed X-Original-Date: Sat, 12 Sep 2015 21:38:05 +0200 X-Submission-Address: std-cpp-submit@vandevoorde.com Date: Sun, 13 Sep 2015 07:51:24 CST Lines: 72 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-7JS6zoqT2WoG+/2+08U/CZhVkABZf4TLoKcHLHwcwupW5zZXgEBoDomgIc81zKXPf8q9ORa6GAIqJOL!3o1iu31pPlvLaT4e3iDIFhylN0qQLhPatte1zjcur4xxJYdD0Pu7JyjA1Wk= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3515 Xref: csiph.com comp.std.c++:776 On 2015-09-12 19:24, Jonathan Jones wrote: > > Hi folks, > > Look at the following example code: > > template > class Base > { > typedef int ValueType; > }; > > template > struct Derived : public Base > { > Derived(ValueType); > }; > > Derived obj(1); > > This code compiles cleanly with GCC 4.7.2 and Clang 3.5 (Xcode 6.2). > However, it fails to compile with Visual Studio 2013, complaining that it > cannot access private typedef "Base::ValueType". > > Furthermore, if you make the private typedef public: > > template > class Base > { > public: > typedef int ValueType; > }; > > It compiles on all three compilers, but the produces different answers. > GCC and clang result in ValueType=float in the Derived constructor > definition, whereas Visual Studio 2013 results in ValueType=int in the > Derived constructor definition. > > What does the standard say should happen in these cases? Which compiler(s) > are closer to implementing the standard behavior? > > The standard is clear (see C++11 section 14.6.1 paragraph 9) about what > happens when "Base" is not a template, but it seems more vague about when > "Base" is a template (which the example above seems to demonstrate). > > I would use different names when Base::ValueType is not the same as Derived::ValueType. :-) Otherwise, I suspect this is one symptom of VC++ failing to do a proper 2-phase name lookup. When Base is a template, its ValueType should not be visible in the declaration of Derived(ValueType). If VC++ postpones the name lookup until the template is instantiated, it WILL be visible at that point. However, as you say, when Base is not a template its ValueType will shadow the template parameter in Derived. That's one reason for using a different name. Bo Persson -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]