Template meta programming problem with g++

Matthew Moran mmoran3519-ml@yahoo.com
Sun Jul 21 00:22:00 GMT 2013


Hi, I'm new to this list, but I may be able to help. I'm by no means an expert, but I think I'm right in this case.

On 07/19/2013 11:59 AM, Aurélien FILEZ wrote:
> This code compiles on MSVC but not on G++ :
>
> ==================================
>
> template<int I>
> class B
> {
> public:
>     static void SomeMethod()
>     {
>         A::SomeMethod();
>     }
> };
>
> class A
> {
> public:
>     static void SomeMethod();
> };
>
> void test()
> {
>     B<3>::SomeMethod();
> }
>
>
> ==================================
>
> The error with g++ is :
> test.cpp: In static member function "static void B<I>::SomeMethod()":
> test.cpp:7:3: error: "A" has not been declared

Change so class A is before B, and define A::SomeMethod, and code will compile (GCC 4.8.1, both -std=c++98 and -std=c++11)

> Is there is a option to indicate to g++ to evaluate the template
> functions only when a specialization is called, and not before ?

This is instantiation, not specialization. In B::SomeMethod, SomeMethod of class A is called by the qualified name A::SomeMethod. Therefore Qualified Name Lookup is used to find it. First A is looked for, then SomeMethod in A, in the enclosing scope of its use. Therefore, A has to be in scope before B is declared. I doubt there is an option to change this behavior, as it is standard conforming. My guess is this is a defect in MSVC where the names are looked up from what is in scope at the point of instantiation, instead of the point of declaration.

I can't find the place in the Standard where it says this at the moment.

Matt Moran



________________________________
From: Aurélien FILEZ <kinju59@gmail.com>
To: gcc-help@gcc.gnu.org 
Sent: Friday, July 19, 2013 11:59 AM
Subject: Template meta programming problem with g++


Hello,

This code compiles on MSVC but not on G++ :

==================================

template<int I>
class B
{
public:
    static void SomeMethod()
    {
        A::SomeMethod();
    }
};

class A
{
public:
    static void SomeMethod();
};

void test()
{
    B<3>::SomeMethod();
}


==================================

The error with g++ is :
test.cpp: In static member function "static void B<I>::SomeMethod()":
test.cpp:7:3: error: "A" has not been declared

Is there is a option to indicate to g++ to evaluate the template
functions only when a specialization is called, and not before ?

Thank you very much ! 



More information about the Gcc-help mailing list