Code::Blocks Forums

User forums => Help => Topic started by: FocusedWolf on October 10, 2005, 06:12:10 am

Title: Templated friend function + gcc = hell
Post by: FocusedWolf 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
Title: Re: Templated friend function + gcc = hell
Post by: mandrav 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?
Title: Re: Templated friend function + gcc = hell
Post by: byo 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;
}
Title: Re: Templated friend function + gcc = hell
Post by: tiwag 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 ;-)
Title: Re: Templated friend function + gcc = hell
Post by: thomas 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.
Title: Re: Templated friend function + gcc = hell
Post by: FocusedWolf 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
Title: Re: Templated friend function + gcc = hell
Post by: FocusedWolf 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?
Title: Re: Templated friend function + gcc = hell
Post by: Ceniza 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;
}
Title: Re: Templated friend function + gcc = hell
Post by: FocusedWolf 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...