Author Topic: Templated friend function + gcc = hell  (Read 8270 times)

FocusedWolf

  • Guest
Templated friend function + gcc = hell
« on: October 10, 2005, 06:12:10 am »
#include <iostream>

template <class Type>
class HateAnsi
{
   
public:

   HateAnsi()
   {}
   
   friend Type Something(Type k);
   
};

template <class Type>
Type Something(Type k)
{
   return k;
}

int main()
{
   std::cout << "Hello world!" << std::endl;
   return 0;
}

This doesn't work...in gcc...but does in vs.net (ansi is the devil),...so how do i get it to work?

I tried to figure this out, but all i keep finding on the web is peoples posts about how ansi is going against the guidlines or something...eh...

O and whatever the fix, the code has to compile in vs.net no matter what :P

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: Templated friend function + gcc = hell
« Reply #1 on: October 10, 2005, 08:48:49 am »
Code
Compiling: C:\Devel\Untitled1.cpp
C:\Devel\Untitled1.cpp:12: warning: friend declaration `Type Something(Type)' declares a non-template function
C:\Devel\Untitled1.cpp:12: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
Linking console executable: C:\Devel\Untitled1.exe
Process terminated with status 0 (0 minutes, 5 seconds)
0 errors, 2 warnings

It is compiling fine (with a warning though). What do you mean it's not working?
Be patient!
This bug will be fixed soon...

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: Templated friend function + gcc = hell
« Reply #2 on: October 10, 2005, 10:07:44 am »
Have You tried something like that ? (It compiles without warnings on GCC)

Code
#include <iostream>

template <class Type>
Type Something(Type k)
{
   return k;
}

template <class Type>
class HateAnsi
{

public:

   HateAnsi()
   {}

   friend Type Something<Type>(Type k);

};

int main()
{
   std::cout << "Hello world!" << std::endl;
   return 0;
}

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: Templated friend function + gcc = hell
« Reply #3 on: October 10, 2005, 10:34:24 am »
Code
#include <iostream>

template<long B,unsigned long E>
struct pow_helper
{
    static const double value;
};

template<long B,unsigned long E>
const double pow_helper<B,E>::value=B*pow_helper<B,E-1>::value;

template<long B>
struct pow_helper<B,0>
{
    static const double value;
};

template<long B>
const double pow_helper<B,0>::value=1;

template<long B,long E>
struct power
{
    static const double value;
};

template<long B,long E>
const double power<B,E>::value= E<0 ? 1.0/pow_helper<B,E<0 ? -E : E>::value :
                                pow_helper<B,E<0 ? -E : E>::value;

int main()
{
    std::cout << power<10,-3>::value << std::endl;
}

or this sample compiles without warnings too and does something meaningful also ;-)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Templated friend function + gcc = hell
« Reply #4 on: October 10, 2005, 01:34:01 pm »
Your code is ambiguous, and the compiler rightly tells you so (it even tells you what to do to avoid the warning).
You should rather blame vs.net for failing to see that.

all i keep finding on the web is peoples posts about how ansi is going against the guidlines or something...eh...
"guiding declarations", not guidelines.

Try this (about half way down the page): http://www.artima.com/cppsource/simple2.html
Quote
But wait a minute. Is operator<< here a template or not? And do I want it to befriend all specializations of Box or not? No conforming C++ compiler will let you instantiate the Box class and use the associated stream inserter.

I believe that compiler warnings are not a deliberate annoyance (although they sometimes seem to be), but they actually prevent you from shooting your foot. This is especially true if you do things that are ambiguous or may have undefined or unexpected results. These may work on one day and may not work on another.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

FocusedWolf

  • Guest
Re: Templated friend function + gcc = hell
« Reply #5 on: October 10, 2005, 06:12:59 pm »
Thx everyone! ... other websites explained to put the implementation on the bottom (but they were using vs i think)...once i did it like your examples, (which works in vs to!) everything worked out great :D


You know what would be good is if someone made a addin for vs.net that added gcc into it. It'd be possible to cause of how extensible vs is,...you can write your own language even with vs having the options to make projects for it and stuff...i wonder if anyone did this already.

Another way, just for the sake of testing if your code was following ansi, would be to write a syntax checker addin...do any of you even uses vs? maybe it's dumb to talk of such things in a codeblocks forum :P

Edit:

Hmm this is intersting: http://msdn.microsoft.com/chats/transcripts/vstudio/vstudio_022703.aspx

says that next vs.net will be 98% comformant to iso standards, or something like that
« Last Edit: October 10, 2005, 06:30:44 pm by FocusedWolf »

FocusedWolf

  • Guest
Re: Templated friend function + gcc = hell
« Reply #6 on: October 11, 2005, 04:11:52 am »
Doh!

template <class Type>
void SomeThing<Type>::Remove(const Type & data = Type())
{}

template <class Type>
class Something
{
   public:
   
        Something()
        {}
       
        void Remove(const Type & data = Type());
};

int main()
{
   return 0;
}

it looks like if i take out where i do " = Type()" that i get some errors...(works in vs).

Is this not permitted anymore in in gcc?

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Templated friend function + gcc = hell
« Reply #7 on: October 11, 2005, 04:45:17 am »
You should put " = Type()" only in the definition and remove it in the declaration. In other words, it should be specified once and only in the definition.

Try this:

Code
template <class Type>
class Something
{
   public:

        Something()
        {}

        void Remove(const Type & data = Type());
};

template <class Type>
void Something<Type>::Remove(const Type & data)
{}

int main()
{
   return 0;
}

FocusedWolf

  • Guest
Re: Templated friend function + gcc = hell
« Reply #8 on: October 11, 2005, 05:11:35 am »
Thx! vs.net really lets people get away with to much :? ...if it weren't for the cool appearence i probably wouldn't even use it...