Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

c++ - Certain errors in uninstantiated template not reported by 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...