public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Name lookup in templates
@ 2006-07-28 23:18 Michael Heissmeier
  2006-08-01 11:26 ` Peter Doerfler
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Heissmeier @ 2006-07-28 23:18 UTC (permalink / raw)
  To: gcc-help

Hello,


the following code snippet compiles under gcc 3.2.3 but fails to do so using
gcc 4.1.0. It is not clear to me whether this is a problem with gcc or with
my code. 

The compiler tells me that it cannot convert C to an int. It seems that TFunc's
call to A::func is bound at definition time although this call depends on the
template parameter. I therefore would expect it to bind during instantiation.

--------------------------------------------

namespace A { 

int func (int c) { return (c + 1); }

} 

template <typename Type>
int TFunc (Type t) { return A::func (t); }


class C
{
  public:

    int myint;
};


namespace A { 

int func (C c) { return (c.myint + 1); }

} 


int main ()
{

    C x;
    x.myint = 2;

    int y = TFunc (x);
}


--------------------------------------------------------


The reason for this code layout is that the first definition of func 
and the template reside in a header which should be included in many
source files.

The definition of Class C happens in an implementation where 
namespace A is reopened in order to add an according definition for func.

It would be nice if there were a solution without getting rid of the 
namespace which prevents us from having to prefix lots of names.

Any suggestions are welcome.


Michael Heissmeier

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

* Re: Name lookup in templates
  2006-07-28 23:18 Name lookup in templates Michael Heissmeier
@ 2006-08-01 11:26 ` Peter Doerfler
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Doerfler @ 2006-08-01 11:26 UTC (permalink / raw)
  To: Michael Heissmeier; +Cc: gcc-help

Michael Heissmeier wrote:
> Hello,
> 
> 
> the following code snippet compiles under gcc 3.2.3 but fails to do so using
> gcc 4.1.0. It is not clear to me whether this is a problem with gcc or with
> my code. 

FYI, Comeau detects the same error as gcc4.1.0. Given its reputation
this is probably the correct behaviour.

> 
> The compiler tells me that it cannot convert C to an int. It seems that TFunc's
> call to A::func is bound at definition time although this call depends on the
> template parameter. I therefore would expect it to bind during instantiation.
> 
> --------------------------------------------
> 
> namespace A { 
> 
> int func (int c) { return (c + 1); }
> 
> } 
> 
> template <typename Type>
> int TFunc (Type t) { return A::func (t); }
> 
> 
> class C
> {
>   public:
> 
>     int myint;
> };
> 
> 
> namespace A { 
> 
> int func (C c) { return (c.myint + 1); }
> 
> } 
> 
> 
> int main ()
> {
> 
>     C x;
>     x.myint = 2;
> 
>     int y = TFunc (x);
> }
> 
> 
> --------------------------------------------------------
> 
> 
> The reason for this code layout is that the first definition of func 
> and the template reside in a header which should be included in many
> source files.
> 
> The definition of Class C happens in an implementation where 
> namespace A is reopened in order to add an according definition for func.
> 
> It would be nice if there were a solution without getting rid of the 
> namespace which prevents us from having to prefix lots of names.
> 
> Any suggestions are welcome.

My guess why your code doesn't work is that the compiler can't know that
you might be overloading the namespace functions at a later point.
However, if you put the functions in a struct as template member
functions and then specialize them, you'll be fine (You could use a
templated struct and specialize that as well).
Note that this is just my idea of why your version is not allowed. Maybe
someone with more Standard foo can pitch in for a better explanation. I
think I read it before but I can't recall where.

Anyway, the below should solve your problem:

namespace A {

  struct func {
    template <typename Type>
    static int use(Type t);
  };

  template<>
  int func::use<int>(int c) {
    std::cout << "func(int)\n";
    return (c + 1);
  }
}

class C
{
  public:
    int myint;
};

template <typename Type>
int Tfunc (Type t) {
  std::cout << "Tfunc(Type)\n";
  A::func::use(t);
}

namespace A {
  template<>
  int func::use<C>(C c) {
    std::cout << "func(C)\n";
    return (c.myint + 1);
  }
}


int main ()
{
    C x;
    x.myint = 2;
    int y = Tfunc (x);
}


> 
> 
> Michael Heissmeier

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

* Name lookup in templates
@ 2004-09-29 21:07 Markus Trenkwalder
  0 siblings, 0 replies; 3+ messages in thread
From: Markus Trenkwalder @ 2004-09-29 21:07 UTC (permalink / raw)
  To: gcc-help

Hi,

   I'm new to this mailing list, so if I'm offending the etiquette,
please excuse me.

I updated to gcc-3.4.1 and try to compile a c++-project I have written.
Some of my code did not compile so I read the "Current Changes" for
gcc-3.4 and found some interesting information.

1) The hint about non-dependent name lookup in templates (example with
foo(int) and foo(char)): Does this mean the order of funtion overloading
does matter?

2) The hint about the unqualified names no longer to be found in
dependent template base classes: What is the reason for this behavior?
(I know this is a question about the standard but maybe someone can tell
me anyway).

TIA

Markus
-- 
:wq

/(bb|[^b]{2})/

Markus Trenkwalder
Student of Computer Engineering at TU-Wien





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

end of thread, other threads:[~2006-08-01 11:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-28 23:18 Name lookup in templates Michael Heissmeier
2006-08-01 11:26 ` Peter Doerfler
  -- strict thread matches above, loose matches on Subject: below --
2004-09-29 21:07 Markus Trenkwalder

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