i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.
i've tested with visual c++ 7 and the comeau online compilers, both
required an explicit cast. is there a way to hint the compiler at using
the cast operator implicitely, or is this not part of the language at
all? (i have a scenario where this would come in very handy, though i
am not sure if i would like c++ to behave like this anyways :)
#include <iostream>
using namespace std;
class test
{
public:
operator ostream& ()
{
return cout;
}
};
void test()
{
test t;
t << "test\n"; // implicit cast doesn't compile
((ostream&)t) << "test\n"; // explicit cast works
} 7 2043 pn*******@gmail .com wrote: i want to use the << operator defined for ostream with an object that itself knows nothing of the operator, but is castable to ostream&. it seems that C++ doesn't do implicit casts before invoking a custom operator.
i've tested with visual c++ 7 and the comeau online compilers, both required an explicit cast. is there a way to hint the compiler at using the cast operator implicitely, or is this not part of the language at all? (i have a scenario where this would come in very handy, though i am not sure if i would like c++ to behave like this anyways :)
#include <iostream> using namespace std;
class test { public: operator ostream& () { return cout; } };
void test() { test t;
t << "test\n"; // implicit cast doesn't compile ((ostream&)t) << "test\n"; // explicit cast works }
Instead of having test implement an ostream conversion operator, why
not simply write a global << operator for an ostream (on the left) and
a test (on the right)?
Greg
"pn*******@gmai l.com" wrote: i want to use the << operator defined for ostream with an object that itself knows nothing of the operator, but is castable to ostream&. it seems that C++ doesn't do implicit casts before invoking a custom operator.
An operator is nothing else then an ordinary function with some strange syntax.
So when your compiler sees.
test t;
t << "test\n"; // implicit cast doesn't compile
it transforms this into:
t.operator<<( "test\n" );
Then the compiler scans the class test, searching for a function operator<<.
Finding none, the compiler searches the freestanding functions for a standalone
.... operator<<( test&, const char* );
Finding none, the compiler gives up and emits an error message.
The problem with that is that the compiler doesn't try to cast an
object to something else, in order to be able to come up with a function
it can call. Either that object's class (or one of its base classes) has
that function or it has not.
--
Karl Heinz Buchegger kb******@gascad .at pn*******@gmail .com wrote: i want to use the << operator defined for ostream with an object that itself knows nothing of the operator, but is castable to ostream&. it seems that C++ doesn't do implicit casts before invoking a custom operator.
Only if the operator is declared a member. The compiler is not required
to apply all possible conversions to try to find which conversion would
be necessary to _then_ resolve a member function call.
i've tested with visual c++ 7 and the comeau online compilers, both required an explicit cast. is there a way to hint the compiler at using the cast operator implicitely, or is this not part of the language at all?
There is no way to "hint".
(i have a scenario where this would come in very handy, though i am not sure if i would like c++ to behave like this anyways :)
#include <iostream> using namespace std;
class test { public: operator ostream& () { return cout; } };
void test() { test t;
t << "test\n"; // implicit cast doesn't compile ((ostream&)t) << "test\n"; // explicit cast works }
Use what works.
V
thanks for the prompt answer. it's clear to me now why implicit cast
doesn't make any sense here, thinking of operators being nothing more
than fancy method invokation syntax...
because the given code is just a sample for my problem. i have a class
with lots of global << operator definitions all over the place, and i
would like to be able to use those on another (unrelated) class without
redefining all those operators... pn*******@gmail .com wrote: thanks for the prompt answer. it's clear to me now why implicit cast doesn't make any sense here, thinking of operators being nothing more than fancy method invokation syntax...
Actually, if operator<< with 'ostream&' as LHS and 'const char*' as RHS
*were* a non-member, the conversion would be considered. Take a look:
struct A {
void operator + (const char*);
};
void operator - (A&, const char*);
// operator+ is a member, operator- is not
struct B {
operator A& ();
};
int main() {
B b;
b + "abc"; // error here
b - "abc"; // _no_ error here
}
V
i see, very interesting. actually my problem was about the usage of
non-member operator functions, i was just unknowingly using the member
operator << defined in ostream& in my test program, not recognizing the
difference between member and non-member operator function definitions.
thanks alot! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Simon Ford |
last post by:
Hi All,
I'm having trouble understanding exactly how I can do some specific implicit
casting. There are two problems here; does anyone know what I should be
doing?
//----------
// (1) Resolving as bool for comparison, but not as int.
//
// we have a class
|
by: Walter Roberson |
last post by:
If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
The C89 standard has some reference to undefined behaviour if one
realloc()'s memory that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
I had always assumed it would automatically free the previous memory,
but is the behaviour...
|
by: Kel Good |
last post by:
Hello,
I recently created a structure in C# that offers several implicit
conversion, hoping I could then reference the C# project from a VB.Net
project, and use this object in VB.Net. It does not seem to work.
I know you cannot create implict conversion in VB.Net, but is the feature
also only useable from C#?
Thanks.
|
by: Steve Gough |
last post by:
Could anyone please help me to understand what is happening here? The
commented line produces an error, which is what I expected given that
there is no conversion defined from type double to type Test. I
expected the same error from the following line, but it compiles fine.
The double is silently truncated to an int and then fed in to the
implicit conversion operator. Why does this happen? Is there any way
that I can keep the implicit...
|
by: Jon Shemitz |
last post by:
Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?
Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?
Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls
true or false operators. I'm inclined...
| |
by: Aaron Queenan |
last post by:
Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
|
by: Chad Z. Hower aka Kudzu |
last post by:
I have an implicit conversion set up in an assembly from a Stream to
something else. In C#, it works. In VB it does not.
Does VB support implicit conversions? And if so any idea why it would work in
a C# program but not VB?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
|
by: Pieter Breed |
last post by:
Hi All,
Please excuse me, but the bulk of my post will be a code post. It
describes some weirdness with regards to the implicit casting operator.
The crux of the problem is this:
I want to set a property on a class that takes an interface instance.
I have a class that can cast implicit to a class that impliments said
interface,
but the compiler moans and says it cannot cast implicitly like that.
|
by: not_a_commie |
last post by:
I have an Angle class that I store angles in. It's basically just a
bunch of fancy functions for manipulating a double. It has implicit
casts for converting to/from double. Due to the project structure and
where it stores the preferred SI units for display, I wanted to have
the TypeConverter in a different project that has no reference to the
angle class. However, returning a double from the ConvertFrom function
throws an error that it...
|
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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |