Thread: anti-standard code can compile -- about template class

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    anti-standard code can compile -- about template class

    Hello everyone,


    According to standard, the code should not compile.

    http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

    Code:
          template <typename T> struct Base {
             int i;
           };
         
           template <typename T> struct Derived : public Base<T> {
             int get_i() { return i; }
           };
    But it can compile without any issues in Visual Studio 2008, without any warnings and even if I select disable language extensions to yes.

    Any comments? What is wrong?


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably a case of "non-standard behaviour" [assuming that your test-code is correctly "non-standard". All compilers attempt to follow the standard, but there are usually some cases where the standard is NOT followed, either as a result of a bug/mistake, or because the compiler provider decided to "break" the standard - and it may be too little reason and too much work to re-implement the standard behaviour as a "choice", in which case the "break" of the standard is just recorded in some document [if it's not, then it's considered a bug, but the resolution may still be to document it, rather than "fix" it].

    You may want to report the bug to Microsoft, but don't expect them to fix it quickly. It is a weird corner-case, and I doubt very much that it's a particularly common source of bugs - have you observed bugs related to this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Seems like Visual Studio is wrong. Please look at section 35.19 of,

    http://www.parashift.com/c++-faq-lit...html#faq-35.19

    1.

    I think this line of statement matters, agree?

    --------------------
    the compiler does not look in dependent base classes (like B<T> when looking up nondependent names (like f).
    --------------------

    2.

    What disadvantage do we have if the code can compile?


    regards,
    George

    Quote Originally Posted by matsp View Post
    Probably a case of "non-standard behaviour" [assuming that your test-code is correctly "non-standard". All compilers attempt to follow the standard, but there are usually some cases where the standard is NOT followed, either as a result of a bug/mistake, or because the compiler provider decided to "break" the standard - and it may be too little reason and too much work to re-implement the standard behaviour as a "choice", in which case the "break" of the standard is just recorded in some document [if it's not, then it's considered a bug, but the resolution may still be to document it, rather than "fix" it].

    You may want to report the bug to Microsoft, but don't expect them to fix it quickly. It is a weird corner-case, and I doubt very much that it's a particularly common source of bugs - have you observed bugs related to this?

    --
    Mats

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks Mats,

    2.

    What disadvantage do we have if the code can compile?
    That it uses a different variable than we expect. In the code you posted, it should give an error because there is no global variable i, and it uses the Base<T> variable i - if this was a real case where you actually wanted it to use the global variable i, it would not work correctly, because it uses the i from inside Base<T>.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    George - 1. That's the jist of it: be explicit (if) when this happens. Being explicit isn't fundamentally wrong.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by George2 View Post
    What disadvantage do we have if the code can compile?
    The main disadvantages are either that the compiler will find a wrong "i" (and therefore the code may execute incorrectly) or that there is a latent bug in the code that might only be found when the code is fed to another compiler.

    Such are the joys when any compiler supports behaviour non-compliant with the standard.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    I agree.

    Quote Originally Posted by matsp View Post
    That it uses a different variable than we expect. In the code you posted, it should give an error because there is no global variable i, and it uses the Base<T> variable i - if this was a real case where you actually wanted it to use the global variable i, it would not work correctly, because it uses the i from inside Base<T>.

    --
    Mats

    What do you mean below, citizen? Sorry English is not my native language and after reading a couple of times, I still do not know what do you mean, could you say in some other words? :-)

    Also, I can not look up the word jist in my dictionary. What does it mean?

    Quote Originally Posted by citizen View Post
    George - 1. That's the jist of it: be explicit (if) when this happens. Being explicit isn't fundamentally wrong.

    Thanks grumpy,


    I agree.

    Quote Originally Posted by grumpy View Post
    The main disadvantages are either that the compiler will find a wrong "i" (and therefore the code may execute incorrectly) or that there is a latent bug in the code that might only be found when the code is fed to another compiler.

    Such are the joys when any compiler supports behaviour non-compliant with the standard.

    regards,
    George

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "jist" means "summary" or "concentrated form of a message".

    In this case, to avoid problems with compilers doing this differently, use explicit forms, e.g. ::i or Base<T>::i to get the compiler to choose "the right thing".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Your description is clear. I think ::i will get the global i. Right?

    Quote Originally Posted by matsp View Post
    "jist" means "summary" or "concentrated form of a message".

    In this case, to avoid problems with compilers doing this differently, use explicit forms, e.g. ::i or Base<T>::i to get the compiler to choose "the right thing".

    --
    Mats

    regards,
    George

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, ::i will give you the "global scope".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Quote Originally Posted by matsp View Post
    Yes, ::i will give you the "global scope".

    --
    Mats
    Cool! Question answered.


    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Can you compile the following template function code?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 03-06-2008, 08:16 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM