Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Certain errors in uninstantiated template not reported by g++

Tags:

c++

templates

g++

Consider this example:

class A
{
  void foo();
  public:
  void bar();
};

template <class> class B
{
  B()
  {
    A a;
    a.foo();    // 1
    A::bar();   // 2
    a.bar(1);   // 3
  }
};

Note B is never instantiated.

clang++ reports all three marked lines as erroneous. g++ (4.8.3) accepts lines 1 and 2 and only reports line 3.

If B is instantiated, g++ happily reports all three lines as erroneous.

Is this a g++ bug? One would think so. A is not a dependent name and its members should be checked normally at template definition time. Are there nuances I don't see?

like image 836
n. 1.8e9-where's-my-share m. Avatar asked Jul 24 '14 12:07

n. 1.8e9-where's-my-share m.


1 Answers

Those pre-instantiation messages aren't enforced by the standard and are up to the compiler

n3337 § 14.6 - 8

No diagnostic shall be issued for a template definition for which a valid specialization can be generated. If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required.

emphasis mine

like image 111
Marco A. Avatar answered Sep 20 '22 12:09

Marco A.