Inheriting specialised templates

Jul 20, 2009 at 3:52pm

I have a problem with the portability of inherited template specialisation syntax (GCC and MS VS C++)

The following short example compiles and runs, and the question is placed in comments in the source.

Can anyone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

    template <class c> class BASE;
    
    template<>
    class BASE<int>
    {
      int D;
    public:
      void Foo(){ std::cout << "In BASE<int>Foo()\n"; }; 
    };
    
    template<>
    class BASE<float>
    {
      float D;
      float E;
    public:
      void Foo(){ std::cout << "In BASE<float>Foo()\n"; };     
    };  
    
    template <class c>    
    class DERIVED
    : public BASE<c>
    {
     public:
//    Both the GCC C++ compiler, and the Microsoft 
//    compiler, accept this syntax:
      void RunAny ( ) { BASE<c>::Foo(); };
#ifdef WIN32  
//    The Microsoft compiler will also accept the following:
      void RunMs ( ) { Foo(); };
//    It is shorter, and hence more convenient,
//    than the version above. However, the GCC compiler 
//    flags it as a syntax error.

//    Is the second (short) version ANSI standard C++? 
//    What are the applicable sections of the ANSI standard?
#endif      
    };    
    
    void main
    (
    )
    {
      DERIVED<int>    I;
      DERIVED<float>  F;
      
      I.RunAny();
      F.RunAny();
#ifdef WIN32        
      I.RunMs();
      F.RunMs();
#endif      
    }
Last edited on Jul 20, 2009 at 4:03pm
Jul 20, 2009 at 5:05pm
Jul 20, 2009 at 5:39pm
Thank you - bang on. Do you happen to know whiich section of the C++ standard states this?

Fuck you Microsoft for your shite compiler. I now have three days work correcting my source code!
Last edited on Jul 20, 2009 at 5:39pm
Jul 20, 2009 at 8:34pm
This extract from section 14.6 should shine a little light - template explaination part
of the specification is really tough going:

//start extract

If a name does not depend on a template-parameter (as defined in 14.6.2), a declaration (or set of declarations)
for that name shall be in scope at the point where the name appears in the template definition;
the name is bound to the declaration (or declarations) found at that point and
this binding is not affected by declarations that are visible at the point of instantiation.
[Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void f(char);
template<class T> void g(T t)
{
f(1); // f(char)
f(T(1)); //dependent
f(t); //dependent
dd++; //not dependent
// error: declaration for dd not found
}
void f(int);
double dd;
void h()
{
g(2); //will cause one call of f(char) followed
// by two calls of f(int)
g(’a’); //will cause three calls of f(char)
}

end example]


//end extract
Jul 21, 2009 at 11:44am
Thank you Dunoas and gestguikas. You have been very helpful.
Topic archived. No new replies allowed.