AdrianH 1,251
Recognized Expert Top Contributor
ok, i've got a template that takes a pointer: -
template<class T>
-
struct deref {
-
T* pObj;
-
deref(T* pObj) : pObj(pObj) {}
-
operator T const & () const { return *pObj; }
-
};
-
and I have the following 2 code snippits: -
deref<int> a(new int(2));
-
cout << endl << string("hello: ") << a << endl;
-
-
deref<string> a(new string("there"));
-
cout << endl << string("hello: ") << a << endl;
-
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: -
ostream& fn(ostream& os, string const & rhs)
-
{
-
os << rhs;
-
return os;
-
}
-
and change my test to the following: -
deref<string> a(new string("there"));
-
cout << endl << string("hello: ");
-
fn(cout, a) << endl;
-
then it works.
I change the function to a template like so: -
template<class T>
-
ostream& fn(ostream& os, T const & rhs)
-
{
-
os << rhs;
-
return os;
-
}
-
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
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: -
#include<iostream>
-
#include<string>
-
using namespace std;
-
-
#define T string
-
//template<class T>
-
struct deref {
-
T* pObj;
-
deref(T* pObj) : pObj(pObj) {}
-
//T & operator ()() const { return *pObj; }
-
operator T & () const { return *pObj; }
-
};
-
#undef T
-
-
#define T deref
-
//template<class T>
-
ostream& fn(ostream& os, T const & rhs)
-
{
-
os << rhs; //< Error occurs here!
-
return os;
-
}
-
#undef T
-
-
int main() {
-
deref/*<string>*/ a(new string("there"));
-
cout << endl << string("hello: ");
-
fn(cout, a) << endl;
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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....
|
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
|
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?
|
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...
|
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...
| |
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
|
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>
|
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:
|
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
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |