public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/20011] New: Templates and cyclic dependencies
@ 2005-02-16 20:38 jmoro at latentzero dot com
  2005-02-16 20:44 ` [Bug c++/20011] " jmoro at latentzero dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jmoro at latentzero dot com @ 2005-02-16 20:38 UTC (permalink / raw)
  To: gcc-bugs

Hello, the new support of two-stage name-lookup for templates in gcc 3.4.0 for
C++ brings a problem that
I don't know how to resolve.
I would like to know what is your recommended way of dealing with the following.


Here is some code that used to compile and link on gcc 3.3 (sorry to copy some
code but that's key to understanding the problem)

///////
// A.h
///////
class B;

class A
{
public: 
 
  A(){}
  ~A(){}

  template <class T> int f( T t )
  {
    B b;
    return ( b.getInt() + t );
  }

  int getInt() const
  {
    return 1;
  }
};


//////
//B.h
//////
class A;

class B
{
public:

  B(){}
  ~B(){}

  template <class T> int g( T t )
  {
    A a;
    return ( a.getInt() + t );
  }

  int getInt() const
  {
    return 2;
  }
};


///////////
//Main.cpp
///////////
#include "A.h"
#include "B.h"

int main( void )
{
  A a;
  int i = 1;
  a.f( i );

  B b;
  b.g( i );
}


As you can see in A.h, the template method f() uses B and in B.h, the template
method g() uses A.
As these methods were only compiled at instantiation time (with gcc 3.3) the
fact that A uses B and B uses A was not a problem but if I compile the same code
with gcc 3.4

I have:

> gcc -v
Reading specs from /var/gnu/3.4.0/lib/gcc/sparc-sun-solaris2.8/3.4.0/specs
Configured with: ../gcc-3.4.0/configure --prefix=/var/gnu/3.4.0 --disable-nls
--enable-languages=c,c++
Thread model: posix
gcc version 3.4.0


>  gcc -c -save-temps Main.cpp
In file included from Main.cpp:1:
A.h: In member function `int A::f(T)':
A.h:13: error: invalid use of undefined type `struct B'
A.h:1: error: forward declaration of `struct B'

This is normal as with 3.4, B needs to be known at definition time in A.h as it
is not dependent on a template parameter.
But obvioulsy, if I change my code so that A.h includes B.h, and B.h includes
B.h, I have a cyclic dependency and it won't compile.

The next step in order to remove such a dependency is usually to get the method
code out of the header file and put it in the cpp file. But of course, this is a
template method and the only method I know to do this with templates is to use
the keyword export... that you don't support.

This last point is fair enough as lots of compilers don't support it but I would
like to know what is you recommended solution to fix this problem?

-- 
           Summary: Templates and cyclic dependencies
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P1
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jmoro at latentzero dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20011


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/20011] Templates and cyclic dependencies
  2005-02-16 20:38 [Bug c++/20011] New: Templates and cyclic dependencies jmoro at latentzero dot com
@ 2005-02-16 20:44 ` jmoro at latentzero dot com
  2005-02-16 21:59 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jmoro at latentzero dot com @ 2005-02-16 20:44 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jmoro at latentzero dot com  2005-02-16 18:06 -------
Created an attachment (id=8204)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=8204&action=view)
source code and *.ii files


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20011


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/20011] Templates and cyclic dependencies
  2005-02-16 20:38 [Bug c++/20011] New: Templates and cyclic dependencies jmoro at latentzero dot com
  2005-02-16 20:44 ` [Bug c++/20011] " jmoro at latentzero dot com
@ 2005-02-16 21:59 ` pinskia at gcc dot gnu dot org
  2005-02-17 16:18 ` jmoro at latentzero dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-02-16 21:59 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-02-16 18:52 -------
The easy way to fix this is to make the template function not be in the class definition.

Anyways if you read a good C++ book, this would be explained there.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20011


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/20011] Templates and cyclic dependencies
  2005-02-16 20:38 [Bug c++/20011] New: Templates and cyclic dependencies jmoro at latentzero dot com
  2005-02-16 20:44 ` [Bug c++/20011] " jmoro at latentzero dot com
  2005-02-16 21:59 ` pinskia at gcc dot gnu dot org
@ 2005-02-17 16:18 ` jmoro at latentzero dot com
  2005-02-17 16:30 ` giovannibajo at libero dot it
  2005-02-17 16:42 ` jmoro at latentzero dot com
  4 siblings, 0 replies; 6+ messages in thread
From: jmoro at latentzero dot com @ 2005-02-17 16:18 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jmoro at latentzero dot com  2005-02-17 10:47 -------
I am sorry but that is not good enough. If I read correcly, you recommend that
to solve the problem with my template function member of a class is ... not to
use template functions as members of the class.

My example was indeed simple and you could get the template out of the class but
if I alter slightly B.h, you can see why I want g() to be a member of the class.

I can obviouly come up with solutions where I don't use member template
functions or even where I don't use templates at all but what I want to know is
how you are supposed to deal with this problem at hand.

What is the recommended way, with gcc 3.4, to deal with template methods and
circular dependencies?

Again, when I read a good C++ book, the solution is to use the export keyword
but GCC does not support it.

///////
// B.h
///////
class A;

class B
{
public:

  B(){}
  ~B(){}

  template <class T> int g( T t )
  {
    A * a = GetA();
    return ( a->getInt() + t );
  }

  int getInt() const
  {
    return 2;
  }
  
private:

  A * GetA();
  
};

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20011


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/20011] Templates and cyclic dependencies
  2005-02-16 20:38 [Bug c++/20011] New: Templates and cyclic dependencies jmoro at latentzero dot com
                   ` (2 preceding siblings ...)
  2005-02-17 16:18 ` jmoro at latentzero dot com
@ 2005-02-17 16:30 ` giovannibajo at libero dot it
  2005-02-17 16:42 ` jmoro at latentzero dot com
  4 siblings, 0 replies; 6+ messages in thread
From: giovannibajo at libero dot it @ 2005-02-17 16:30 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From giovannibajo at libero dot it  2005-02-17 11:58 -------
Maybe it is not very clear to you, but this is the GCC Bugzilla. This is where 
people report *bugs* of the GCC compiler. It is not a help desk for C++ 
programmers. If you have a BUG to report about GCC, we're eager to know. If you 
do not know how to write your program so that it follows all the ISO C++ rules, 
we really cannot help. Try asking in a newsgroup like comp.lang.c++.moderated.

Anyway, the solution for your problem is very easy, you just need to define 
your member functions outside the class definition.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20011


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/20011] Templates and cyclic dependencies
  2005-02-16 20:38 [Bug c++/20011] New: Templates and cyclic dependencies jmoro at latentzero dot com
                   ` (3 preceding siblings ...)
  2005-02-17 16:30 ` giovannibajo at libero dot it
@ 2005-02-17 16:42 ` jmoro at latentzero dot com
  4 siblings, 0 replies; 6+ messages in thread
From: jmoro at latentzero dot com @ 2005-02-17 16:42 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jmoro at latentzero dot com  2005-02-17 12:11 -------
Fair enough, you can indeed resolve the circular dependencies by moving the
definition but you still have more dependencies between files than you used to
with 3.3.

It's all good to follow the standard but implementing the two phase lookup
without implementing the export keyword makes it difficult to minimize the
dependencies.

Anyway, thanks for your "help"

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20011


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-02-17 12:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-16 20:38 [Bug c++/20011] New: Templates and cyclic dependencies jmoro at latentzero dot com
2005-02-16 20:44 ` [Bug c++/20011] " jmoro at latentzero dot com
2005-02-16 21:59 ` pinskia at gcc dot gnu dot org
2005-02-17 16:18 ` jmoro at latentzero dot com
2005-02-17 16:30 ` giovannibajo at libero dot it
2005-02-17 16:42 ` jmoro at latentzero dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).