• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: C++ Templates compilation problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ Templates compilation problem


  • Subject: Re: C++ Templates compilation problem
  • From: James Walker <email@hidden>
  • Date: Thu, 10 Feb 2011 10:20:33 -0800

On 2/10/2011 9:14 AM, Damian Wilson wrote:
Hi Xcode-users

I'm having an issue compiling some source code that I've ported over
from Windows. It's one of those things where I've spent a long time
staring at it and can't work out why it doesn't like it.

I'm building on an iMac, Xcode version 3.2.5, GNU 4.2.1.

Here is the code:
#include <string>

template<class T>
class CompoundObject
{
public:
CompoundObject(int obj1, T* obj2)
: m_object1(obj1), m_object2(obj2) {}

int m_object1;
T* m_object2;
};

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

template<class T> Streamer& operator>>(CompoundObject<T>& c)
{
printf("Obj1 = %d\n", c.m_object1);
return *this;
}
};

template<class T> inline
CompoundObject<T> MakeCompoundObject(int obj1, T* obj2)
{
return CompoundObject<T>(obj1, obj2);
}

int _tmain(int argc, _TCHAR* argv[])
{
Streamer s;
std::string object("Hello");
s >> MakeCompoundObject(1, &object); <== ERROR
return 0;
}

The error returned is:
/Volumes/Development/TemplateSample/TemplateSample.cpp: In function 'int
_tmain(int, char**)':
/Volumes/Development/TemplateSample/TemplateSample.cpp:47: error: no
match for 'operator>>' in 's >> MakeCompoundObject [with T =
std::string](1, (& object))'
/Volumes/Development/TemplateSample/TemplateSample.cpp:29: note:
candidates are: Streamer& Streamer::operator>>(CompoundObject<T>&) [with
T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]

The weird thing about the error is it identifies the correct candidate
function, but is then refusing to use it. If I create a temporary
variable to hold the result of MakeCompoundObject, and then call "s >>
temporary", it compiles fine but that is rather spoiling the brevity of
some of our code.


The problem has to do with the fact that you're passing a temporary object by reference. If you make it a const reference, i.e.,

operator>>(const CompoundObject<T>& c)

then it works.
--
  James W. Walker, Innoventive Software LLC
  <http://www.frameforge3d.com/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >C++ Templates compilation problem (From: Damian Wilson <email@hidden>)

  • Prev by Date: C++ Templates compilation problem
  • Next by Date: Re: Adding a target dependency
  • Previous by thread: C++ Templates compilation problem
  • Next by thread: [ANN] Cong 0.7
  • Index(es):
    • Date
    • Thread