 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kaba Guest
|
Posted: Tue Dec 06, 2005 4:29 am Post subject: Imitating class namespaces |
|
|
Hello
While waiting for something like class namespaces
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1420.pdf
I have been looking for other methods to separate template class
declaration from the member function definitions without having to write
lots of template parameter lines for each out-of-class function
definition.
So I was thinking if this was possible:
class A
{
public:
// Declarations
void f();
// Definitions
void f()
{
// Code here
}
};
In analogy to other entities, you could declare many times, but only
give at most one definition (ODR). Quick compilation with Comeau C++ and
VC++2005 gave an error. Is this correct according to the standard?
--
Kalle Rutanen
http://kaba.hilvi.org
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Wed Dec 07, 2005 3:36 am Post subject: Re: Imitating class namespaces |
|
|
Kaba wrote:
| Quote: |
So I was thinking if this was possible:
class A
{
public:
// Declarations
void f();
// Definitions
void f()
{
// Code here
}
};
In analogy to other entities, you could declare many times, but only
give at most one definition (ODR). Quick compilation with Comeau C++ and
VC++2005 gave an error. Is this correct according to the standard?
|
Yes it's correct. It's in 9.2/1: "A member shall not be declared
twice in the member-specification, except that a nested class or member
class template can be declared and then later defined."
BTW, what's the point in separating declarations from definitions at
class scope? It has no semantic value as members are visibile in the
whole class scope anyway, so I guess it's just a nice way of organizing
the code. However, it may lead to unexpected results if the declaration
and definition are not kept in sync with each other. Consider this:
--- example1.cpp
// *assuming members can multiply declared* (currently illegal)
class A
{
public:
// Declarations
void f();
// Definitions
void f()
{
// Code here
}
};
void test(A& a)
{
a.f(); // ok, calls A::f()
}
--- end example1.cpp
now someone changes the definition of f() but forgets to keep the
declaration in sync:
--- example2.cpp
class A
{
public:
// Declarations
void f();
// Definitions
void f() const // <<< CHANGE HERE
{
// Code here
}
};
void test(A& a)
{
a.f(); // still compiles ok, but gives link error!
}
--- end example2.cpp
Using overloads it is also possible to devise non-trivial cases where
the user code silently changes behaviour.
Just my 2 eurocent,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
vish Guest
|
Posted: Wed Dec 07, 2005 3:37 am Post subject: Re: Imitating class namespaces |
|
|
f() has 2 declarations in the same declarative scope. There's ambiguity
here, since name of the function is same the compiler will treat this
as overload but due to same signature, the overload will fail, this
will be treated as a case of redeclaration of member function.
As per the standard "Except for member function definitions that appear
outside of a class definition, and except for explicit specializations
of member functions of class templates and member function templates
(14.7) appearing outside of the class definition, a member function
shall not be redeclared."
-Vish
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Kaba Guest
|
Posted: Wed Dec 07, 2005 3:53 pm Post subject: Re: Imitating class namespaces |
|
|
Thanks Alberto and vish, I will reply to both here.
| Quote: | Yes it's correct. It's in 9.2/1: "A member shall not be declared
twice in the member-specification, except that a nested class or member
class template can be declared and then later defined."
|
Ok.
| Quote: |
BTW, what's the point in separating declarations from definitions at
class scope? It has no semantic value as members are visibile in the
whole class scope anyway, so I guess it's just a nice way of organizing
the code. However, it may lead to unexpected results if the declaration
and definition are not kept in sync with each other. Consider this:
|
Nice example. So the original idea is not good.
The point is that I could *physically* have the member declarations
grouped in the class definition, so it is easy to see the class'
interface. So why not use out-of-class definitions? Because I do a lot
of template programming, the template parameter writing for each out-of-
class function definition sometimes makes the code look very noisy. For
one line member function there can be ten lines of telling the compiler
what specific class I am meaning. I hope class namespaces will help with
this on the next standard. While waiting for that one has to make up
something else.
--
Kalle Rutanen
http://kaba.hilvi.org
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Thu Dec 08, 2005 7:00 am Post subject: Re: Imitating class namespaces |
|
|
Kaba wrote:
| Quote: | The point is that I could *physically* have the member declarations
grouped in the class definition, so it is easy to see the class'
interface. So why not use out-of-class definitions? Because I do a lot
of template programming, the template parameter writing for each out-of-
class function definition sometimes makes the code look very noisy. For
one line member function there can be ten lines of telling the compiler
what specific class I am meaning. I hope class namespaces will help with
this on the next standard. While waiting for that one has to make up
something else.
|
In the meantime, what you can do is to simply put your declarations
inside comments. The compiler could not have checked for consistency
between declarations and definitions anyway (as shown in my example).
HTH,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
johnchx2@yahoo.com Guest
|
Posted: Thu Dec 08, 2005 7:05 am Post subject: Re: Imitating class namespaces |
|
|
At the risk drifting off-topic, I'll mention that you can accomplish
something like what you're after with a little shameless macro magic:
#define INTERFACE class inner_bits {
#define IMPLEMENTATION };
template < class T, class U, int i >
struct example {
INTERFACE
int foo( T t, U u);
IMPLEMENTATION
int foo( T t, U u) { return i; }
};
int main()
{
example< int, int, 0 > e;
e.foo(10,20);
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Kaba Guest
|
Posted: Thu Dec 08, 2005 3:37 pm Post subject: Re: Imitating class namespaces |
|
|
Again, replying to both repliers Alberto and John.
Thank you for your suggestions. The declaration commenting approach has
a few problem: it does not look good, it messes text editor color coding
and is not checked by the compiler. The inner class approach takes care
of all the aforementioned problems. Problem solved:)
In article <1134021215.720285.34200 (AT) g49g2000cwa (DOT) googlegroups.com>,
[email]johnchx2 (AT) yahoo (DOT) com[/email] says...
| Quote: | At the risk drifting off-topic, I'll mention that you can accomplish
something like what you're after with a little shameless macro magic:
#define INTERFACE class inner_bits {
#define IMPLEMENTATION };
template < class T, class U, int i
struct example {
INTERFACE
int foo( T t, U u);
IMPLEMENTATION
int foo( T t, U u) { return i; }
};
int main()
{
example< int, int, 0 > e;
e.foo(10,20);
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
--
Kalle Rutanen
http://kaba.hilvi.org
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|