Topic: Imitating class namespaces


Author: none@here.com (Kaba)
Date: Tue, 6 Dec 2005 04:29:33 GMT
Raw View
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                       ]





Author: "vish" <vishakha@gmail.com>
Date: Tue, 6 Dec 2005 21:37:53 CST
Raw View
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                       ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Wed, 7 Dec 2005 03:36:00 GMT
Raw View
Kaba wrote:
>
> 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                       ]





Author: none@here.com (Kaba)
Date: Wed, 7 Dec 2005 15:53:44 GMT
Raw View
Thanks Alberto and vish, I will reply to both here.

> 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.

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





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 8 Dec 2005 07:00:08 GMT
Raw View
Kaba wrote:
> 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                       ]





Author: johnchx2@yahoo.com
Date: Thu, 8 Dec 2005 01:05:38 CST
Raw View
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                       ]





Author: Kaba <none@here.com>
Date: Thu, 8 Dec 2005 09:37:06 CST
Raw View
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@g49g2000cwa.googlegroups.com>,
johnchx2@yahoo.com says...
> 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                       ]