Re: Is this kind of static polymorphism valid?
[2nd attempt after 24 hours]
Am 03.09.2012 21:17, schrieb DeMarcus:
[..]
I'm experimenting with static polymorphism and came up with the
following. This example do run under gcc 4.7.1 but without luck I've
tried to find the paragraphs in the standard that allow this kind of
static polymorphism.
template<class T>
class Runner
{
public:
void run()
{
runPrivate();
}
private:
void runPrivate();
};
namespace
{
// (struct SomeRunnerLabel without declaration)
Runner<struct SomeRunnerLabel> r;
}
template<>
void Runner<SomeRunnerLabel>::runPrivate()
{
std::cout << "Static polymorphism" << std::endl;
}
int main( int argc, char* argv[] )
{
r.run();
return 0;
}
My questions are:
1. Am I allowed to use struct SomeRunnerLabel as I do here?
Yes.
SomeRunnerLabel is not even a forward declaration and it just serves
as a label to get the polymorphism. Does anyone know where in the
standard I can find the paragraph that allows this kind of template
argument?
I would start with 7.1.6.3 [dcl.type.elab] p2:
"3.4.4 describes how name lookup proceeds for the identifier in an
elaborated-type-specifier. [..] If the elaborated-type-specifier is
introduced by the class-key and this lookup does not find a previously
declared type-name, [..] the elaborated-type-specifier is a declaration
that introduces the class-name as described in 3.3.2."
3.4.4 [basic.lookup.elab] p2 says:
"If the elaborated-type-specifier has no nested-name-specifier, and
unless the elaborated-type-specifier appears in a declaration with the
following form:
class-key attribute-specifier-seqopt identifier ;
the identifier is looked up according to 3.4.1 but ignoring any non-type
names that have been declared. [..] If the elaborated-type-specifier is
introduced by the class-key and this lookup does not find a previously
declared type-name, [..] the elaborated-type-specifier is a declaration
that introduces the class-name as described in 3.3.2."
Finally we have 3.3.2 [basic.scope.pdecl] p6:
"The point of declaration of a class first declared in an
elaborated-type-specifier is as follows:
? for a declaration of the form
class-key attribute-specifier-seqopt identifier ;
the identifier is declared to be a class-name in the scope that contains
the declaration, otherwise [..]"
3.4.1 does not apply, because we have no previously declared type of
that name, thus we follow this change and find that the effect is that
the name SomeRunnerLabel is introduced into the scope of the unnamed
namespace.
2. Is this kind of static polymorphism allowed at all? As we see, the
template parameter T is not used for anything but creating multiple
versions of the class, doing polymorphism on the runPrivate()
function. Also for this one I would be very happy if someone could
direct me to the paragraph that allows this kind of polymorphism.
It depends what you want to do. If the unnamed namespace is part of a
shared header, different translation units will see different
SomeRunnerLabel and different objects r. Is this what you want?
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]