From: Helmut Jarausch on
Hi,

can anybody please explain to me why the two statements marked ??? below
are rejected by current C++. Will there be any change with C++-0X ?

class X {
int a;
static const int Version=123;
public:
X(int Ini) : a(Ini) {}

static void printD(const X& Arg);
};

void X::printD(const X& Arg) {
cerr << "Version " << Version << " : " << Arg.a << std::endl;
}

int main() {
X x(7);
printD(x); // printD not declared ( why not found by ADL ? ) ???

using X::printD; // X is not a namespace : why not possible ???
printD(x);

X::printD(x); // OK
}

Many thanks for an explanation,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 20 Jul., 16:07, Helmut Jarausch <jarau...(a)igpm.rwth-aachen.de>
wrote:
> can anybody please explain to me why the two statements marked ??? below
> are rejected by current C++. Will there be any change with C++-0X ?
>
> class X {
> int a;
> static const int Version=123;
> public:
> X(int Ini) : a(Ini) {}
> static void printD(const X& Arg);

This is a static member function. It can only be invoked
via an object of this class (or derived of) or by a
qualification with the class name (or derived of).

I haven't heart of any movements to change this basic
idea.

> };
>
> void X::printD(const X& Arg) {
> cerr << "Version " << Version << " : " << Arg.a << std::endl;
> }
>
> int main() {
> X x(7);
> printD(x); // printD not declared ( why not found by ADL ? ) ???

ADL does not involve class members. I don't see a convincing reason
why it
should. If you want to realize such syntax you can use a free function
-
that's for what they are good for! (In this case this will be a friend
function,
if you don't want to use delegation)

> using X::printD; // X is not a namespace : why not possible ???

Because a class scope is a closed thing and is not designed to
be pulled into some other space (except into the class scope of a
derived class). Put your entities into a namespace if that's what
you want.

> printD(x);
>
> X::printD(x); // OK
>
> }

I'm not really convinced of the examples. There exist simple
approaches
to change your design to have the same effect, e.g.

namespace N {
class X {
int a;
static const int Version=123;
public:
X(int Ini) : a(Ini) {}
static void printD(const X& Arg);
};
inline void printD(const X& Arg) { X::printD(Arg); }
}

int main() {
N::X x(7);
printD(x); // OK

using N::printD; // OK
printD(x);

N::X::printD(x); // OK
}

There is nothing wrong in using free functions. In
contrast: They seem to become more popular
nowadays. E.g. the new C++0x thread components
that provide the functionality of the current thread
are free functions in a separate namespace:

namespace std {
class thread;
namespace this_thread {
thread::id get_id();
void yield();
template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>&
abs_time);
template <class Rep, class Period>
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
}
}

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! ]