Topic: Implicitly instantiate a pure virtual function?


Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sat, 27 Feb 2010 20:37:43 CST
Raw View
The Standard says at 14.7.1/9

"An implementation shall not implicitly instantiate a function template, a
member template, a non-virtual member function, a member class or a static
data member of a class template that does not require instantiation. 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."

This confuses me a bit, because the following code is well formed

struct A {
 virtual void f() = 0;
};

While the following code is ill-formed if the compiler decides to
instantiate "f" (assuming we don't provide the definition for "f")

template<class>
struct A {
 virtual void f() = 0;
};

struct A1 : A<int> { };

Is this intended? It seems to have ugly consequences on code that tries to
be portable.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Tue, 2 Mar 2010 12:13:23 CST
Raw View
On Feb 28, 3:37 am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> While the following code is ill-formed if the compiler decides to
> instantiate "f" (assuming we don't provide the definition for "f")
>
> template<class>
> struct A {
>  virtual void f() = 0;
>
> };
>
> struct A1 : A<int> { };
>

No, it isn't. What makes you think it is? (But note that A1 is
abstract, like A<int>.)

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Tue, 2 Mar 2010 16:31:52 CST
Raw View
CornedBee wrote:

> On Feb 28, 3:37 am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> wrote:
>> While the following code is ill-formed if the compiler decides to
>> instantiate "f" (assuming we don't provide the definition for "f")
>>
>> template<class>
>> struct A {
>>  virtual void f() = 0;
>>
>> };
>>
>> struct A1 : A<int> { };
>>
>
> No, it isn't. What makes you think it is? (But note that A1 is
> abstract, like A<int>.)
>

Doesn't that paragraph grant the compiler to cause an implicit instantiation
of "f"? According to 2.2/1p8, the instantiation will fail because we won't
find the definition of A<T>::f: "Each translated translation unit is
examined to produce a list of required instantiations. [ Note: this may
include instantiations which have been explicitly requested (14.8.2).        end
note ] The definitions of the required templates are located. It is
implementation-defined whether the source of the translation units
containing these definitions is required to be available. All the required
instantiations are performed to produce instantiation units. The program is
ill-formed if any instantiation fails."

If i make A<T>::f non-abstract and don't define it, GCC fails to link
because the instantiation of A<T>::f fails. Now i don't see 14.7.1/9
differentiationg between virtual functions that are abstract, and non-
abstract virtual functions. Do i miss something obvious?



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sun, 7 Mar 2010 21:39:43 CST
Raw View
On 2 Mrz., 23:31, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> CornedBee wrote:
> > On Feb 28, 3:37 am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> > wrote:
> >> While the following code is ill-formed if the compiler decides to
> >> instantiate "f" (assuming we don't provide the definition for "f")
>
> >> template<class>
> >> struct A {
> >>  virtual void f() = 0;
>
> >> };
>
> >> struct A1 : A<int> { };
>
> > No, it isn't. What makes you think it is? (But note that A1 is
> > abstract, like A<int>.)
>
> Doesn't that paragraph grant the compiler to cause an implicit instantiation
> of "f"? According to 2.2/1p8, the instantiation will fail because we won't
> find the definition of A<T>::f: "Each translated translation unit is
> examined to produce a list of required instantiations. [ Note: this may
> include instantiations which have been explicitly requested (14.8.2).     end
> note ] The definitions of the required templates are located. It is
> implementation-defined whether the source of the translation units
> containing these definitions is required to be available. All the required
> instantiations are performed to produce instantiation units. The program is
> ill-formed if any instantiation fails."
>
> If i make A<T>::f non-abstract and don't define it, GCC fails to link
> because the instantiation of A<T>::f fails. Now i don't see 14.7.1/9
> differentiationg between virtual functions that are abstract, and non-
> abstract virtual functions. Do i miss something obvious?

The instantiation rules in 2.2 do not explicitly say that
a *definition* is required for a successful function
instantiation.

According to [basic.def.odr]/2:

"A virtual member function is used if it is not pure."

so there is no reason to assume that a definition is required.

The actual rule that requires the existing definition of *all*
non-pure virtual function is [class.virtual]/9 (p. 8 in C++03):

"A virtual function declared in a class shall be defined,
or declared pure (10.4) in that class, or both; but no
diagnostic is required (3.2)."

[Thanks to Jens Maurer for pointing me to this paragraph]

So, the problem you are referring to is not related to
template instantiation.

HTH & Greetings from Bremen,

Daniel Kr   gler






--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]