Visual C++ too lenient

This is a discussion on Visual C++ too lenient within the C++ Programming forums, part of the General Programming Boards category; I checked in two mistakes that were immediately seen by a guy running clang. I developed this under VS 2013. ...

  1. #1
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248

    Visual C++ too lenient

    I checked in two mistakes that were immediately seen by a guy running clang. I developed this under VS 2013. I would have though MS had fixed this improper leniency by now:

    Example 1:

    Code:
    template <typename X>
    class Foo
    {
    public:
        template <typename X> // I shadowed the outer template parameter and yet it compiles
        class Bar
        {
        };
    };
    Example 2:

    Code:
    template <typename X>
    class Foo
    {
    public:
        template <typename T>
        void Bar();
    };
    
    Foo<int> x;
    x.Bar<int>(); // I need to write x.template Bar<int>() instead, and yet it compiles
    ARGH!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    The first is obvious and does not compile on GCC as far as I remember, but I'm not sure about the second. I don't have currently access to the standard (nor to compiler) so I won't check, but I would argue that you don't need "template" there, because Foo<int> is fully known to the compiler. "template" keyword for methods is necessary in such scenarios as in DoSomethingWithFoo:
    Code:
    template <typename X>
    class Foo
    {
    public:
    
        template <typename Y>
        void Bar();
    };
    
    template <typename X>
    void DoSomethingWithFoo(Foo<X>& x)
    {
        x.template Bar<int>(); // Type of x is unknown - "Bar" is a dependant name.
    }
    
    Foo<int> x;
    x.Bar<int>(); // Type of x is known.
    Last edited by kmdv; 06-26-2014 at 05:37 AM.
    I never put signature, but I decided to make an exception.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    You're right.. the actual line of code is like this:

    Code:
    return Foo<A>().template B<C, D>();
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio DLL and Visual Basic
    By mathguy in forum C Programming
    Replies: 18
    Last Post: 12-08-2011, 02:55 PM
  2. Visual C++ and Visual Studio 2010
    By 03jh01 in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2010, 04:03 AM
  3. Visual C++ 6.0 Pro vs Visual C++ 6.0 Enterprise
    By Joelito in forum Tech Board
    Replies: 5
    Last Post: 01-23-2007, 06:00 PM
  4. Replies: 1
    Last Post: 04-20-2002, 06:49 AM
  5. Moving from Visual Basic to Visual C++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2002, 09:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21