473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Advanced template autocast problem

AdrianH
1,251 Recognized Expert Top Contributor
ok, i've got a template that takes a pointer:
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. struct deref {
  3.     T* pObj;
  4.     deref(T* pObj) : pObj(pObj) {}
  5.     operator T const & () const { return *pObj; }
  6. };
  7.  
and I have the following 2 code snippits:
Expand|Select|Wrap|Line Numbers
  1.     deref<int> a(new int(2));
  2.     cout << endl << string("hello: ") << a << endl;
  3.  
Expand|Select|Wrap|Line Numbers
  1.     deref<string> a(new string("there"));
  2.     cout << endl << string("hello: ") << a << endl;
  3.  
The first snippet works fine, the second has a compile error saying there is no function that corrisponds.

If I declare a new function like so:
Expand|Select|Wrap|Line Numbers
  1. ostream& fn(ostream& os, string const & rhs)
  2. {
  3.     os << rhs;
  4.     return os;
  5. }
  6.  
and change my test to the following:
Expand|Select|Wrap|Line Numbers
  1.     deref<string> a(new string("there"));
  2.     cout << endl << string("hello: ");
  3.     fn(cout, a) << endl;
  4.  
then it works.

I change the function to a template like so:
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. ostream& fn(ostream& os, T const & rhs)
  3. {
  4.     os << rhs;
  5.     return os;
  6. }
  7.  
and I get the same error.

As I understand it, in my test, the type T is a deref<string>. That said, it has an autocast that should generate a T const & which is string const &.

So my question is why is the autocast not being done?


Adrian
Mar 4 '07 #1
1 2008
AdrianH
1,251 Recognized Expert Top Contributor
Well, I've looked in to it by removing all templates thus having a concreate class deref and function fn() and the problem still exists.

It is like the function ostream& operator << (ostream&, string const &) has been declared as explicit, but explicit is only available for constructors.

Here is my new code:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. #define T string 
  6. //template<class T>
  7. struct deref {
  8.     T* pObj;
  9.     deref(T* pObj) : pObj(pObj) {}
  10.     //T & operator ()() const { return *pObj; }
  11.     operator T & () const { return *pObj; }
  12. };
  13. #undef T
  14.  
  15. #define T deref
  16. //template<class T>
  17. ostream& fn(ostream& os, T const & rhs)
  18. {
  19.     os << rhs;  //< Error occurs here!
  20.     return os;
  21. }
  22. #undef T
  23.  
  24. int main() {
  25.     deref/*<string>*/ a(new string("there"));
  26.     cout << endl << string("hello: ");
  27.     fn(cout, a) << endl;
  28. }
  29.  
The error messages are:
../main.cpp: In function `std::ostream& fn(std::ostream &, const deref&)':
../main.cpp:20: error: no match for 'operator<<' in 'os << rhs'
then spouts a bunch of candidates, none of which take a string const &. And I do know that one does exist as I've seen it in the header files.

Man this is frustrating.

Does anybody have an idea what is going on here?


Adrian
Mar 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2140
by: nortelsale | last post by:
I know nothing about programming but I am building a website (written with php) that allow people post some info. Is there a site I can get some templates or php scripts for an advanced search function? The Advanced Search function shall able to search any posted info by a postal code/zip code within certain kms and be able to search by map by clicking to US/Canada map to get to state, then to cities than to towns....
1
1301
by: David2511 | last post by:
Hello, I need a little help. I try to write the following architecture : an abstract template class A Two classes derived from class A : the classes B and C which are concrete. The class A contains a static method called CreateA which allows to
2
1110
by: Paul | last post by:
Is there a way to explicitly create a template function T whose T is a template class? I would envision the code to look something like this... template <typename T, typename CT<T> > void func(CT<T>& c){} - func would accept template class types. Does anyone know if this is possible?
22
2118
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for what I consider to be some holes in the standard language. How do I go about writing and compiling this without this stupid error about not having a 'main' function. I don't want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would...
1
2078
by: mosscliffe | last post by:
I wanted to install the SQL SERVER 2005 Express Advanced Services, because I wanted the import / export features of the data manager. I chose the route of deleting the template files and then installing, as my version of SQL Server express was part of the install, with Visual Web Developer 2005 Express Edition and I was not confident of re-installing just SQL Server Express. The install progressed quite nicely until I got the following...
2
1802
by: Erich Pul | last post by:
hi! i'm sincerely in need for literature (in form of one or more books if possible) on advanced php topics like template systems, frameworks, MVC, ... can anyone provide a few titles or authors? many thanks in advance, erich
19
7920
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
4
1233
AdrianH
by: AdrianH | last post by:
Hi all, I am working on some specialty template library, trying to write a template class that will not allow a constant type. I've only come up with the following: template <class T> class X { private:
6
18975
by: pavel.repkin | last post by:
Hey! How would you do the following task? Let you have an XML tree on input. Suppose, there is a special kind of node you want to remove. Let it have "bad" name. Each "bad" node has a parent node, obviously. In case there are no children left in the parent after removal of all the "bad" nodes, the parent must also be removed. And this rule is applied to all the ancetors of the "bad" node
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.