[cfe-dev] Instantiating virtual member functions in class template

Dmitri Gribenko gribozavr at gmail.com
Thu Feb 16 14:43:10 PST 2012


Hi,

A component of OpenMPI failed to compile with clang, although it
compiles with gcc, pgi, pathcc, xlc, icc, open64 and suncc (according
to OpenMPI developers).

The code boils down to this (tested to be accepted by gcc 4.6.2,
rejected by clang from svn trunk):
=== cut
template<typename T>
void call_less(T T1, T T2)
{
  T1 < T2;
}

template<typename T>
class C
{
public:
  virtual void a() {
    T T1, T2;
    call_less(T1, T2); // Requires operator<
  }
  virtual void z() {}
};

struct S { int a; }; // No operator<
void test(C<S> *CC)
{
  CC->z(); // this will not instantiate C<S>::a(), will it?
}
=== cut

I suspect that C++11 [temp.inst] p10 applies to this code:
> It is unspecified whether or not an implementation implicitly
> instantiates a virtual member function of a class template
> if the virtual member function would not otherwise be
> instantiated.

and thus clang behavior is conforming, but I would be thankful if
someone commented on this.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/



More information about the cfe-dev mailing list