473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Default conversion when passing to an ellipsis

Hi,

I have a class String which implements the const char*() operator. I
was doing some tests today and did something like:
String test( "hello world" );
printf( "%s\n", test ); // instead of test.c_str(); or (const
char*)test

This code compiled and worked fine on a x86/visual studio 7.1. On some
other platforms, gcc was giving errors, as i would expect.

So i'm wondering if visual studio is allowing something which is
forbidden by the standard, or is there a way to force a default
conversion operator to be called when an object instance is passed to
an ellipsis?

Thanks,

/d

Jun 22 '06 #1
11 3057
Perhaps Visual Studio defines
operator char const *
on strings?

Kufa wrote:
Hi,

I have a class String which implements the const char*() operator. I
was doing some tests today and did something like:
String test( "hello world" );
printf( "%s\n", test ); // instead of test.c_str(); or (const
char*)test

This code compiled and worked fine on a x86/visual studio 7.1. On some
other platforms, gcc was giving errors, as i would expect.

So i'm wondering if visual studio is allowing something which is
forbidden by the standard, or is there a way to force a default
conversion operator to be called when an object instance is passed to
an ellipsis?

Thanks,

/d


Jun 22 '06 #2
abose wrote:
Perhaps Visual Studio defines
operator char const *
on strings?


This is a custom String class.
/k

Jun 22 '06 #3
Kufa wrote:
I have a class String which implements the const char*() operator. I
was doing some tests today and did something like:
String test( "hello world" );
printf( "%s\n", test ); // instead of test.c_str(); or (const
char*)test

This code compiled and worked fine on a x86/visual studio 7.1. On some
other platforms, gcc was giving errors, as i would expect.

So i'm wondering if visual studio is allowing something which is
forbidden by the standard, or is there a way to force a default
conversion operator to be called when an object instance is passed to
an ellipsis?


If you need Visual-C++-specific information, please consider asking
in 'microsoft.public.vc.language'.

Since 'String' here is an object, passing it to a function with the
ellipsis causes undefined behaviour (5.2.2/7).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '06 #4
Kufa wrote:
...
I have a class String which implements the const char*() operator. I
was doing some tests today and did something like:
String test( "hello world" );
printf( "%s\n", test ); // instead of test.c_str(); or (const
char*)test

This code compiled and worked fine on a x86/visual studio 7.1. On some
other platforms, gcc was giving errors, as i would expect.

So i'm wondering if visual studio is allowing something which is
forbidden by the standard, or is there a way to force a default
conversion operator to be called when an object instance is passed to
an ellipsis?
...


Neither. Strictly speaking, class types are allowed as arguments for
'...' parameters, meaning that this particular piece of code is
well-formed and no diagnostics is required. However, with class types
the behavior is defined only when the class type is a POD. Your class
type is clearly not a POD, which means that the behavior is undefined.

From that point of view, VS compiler is doing nothing wrong. It is not
required to generate a diagnostic message and it is allowed to do
interpret the code in any way it pleases.

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #5
Kufa wrote:
I have a class String which implements the const char*() operator. I was
doing some tests today and did something like: String test( "hello world"
);
printf( "%s\n", test ); // instead of test.c_str(); or (const char*)test
The test object drops in as an object; a copy of it appears in printf's
stack's arguments.

If your string class starts with char *, then %s will accidentally see the
address of your string, which it expects. This is still undefined behavior.

(Interestingly, Microsoft's CString class was tweaked to permit
programmers to pass it like this. Such behavior lies between undefined and
implementation-defined. MS gave up trying to educate programmers not to
pass strings into ellipses like that.)

Don't pass strings into ellipses like that.
This code compiled and worked fine on a x86/visual studio 7.1. On some
other platforms, gcc was giving errors, as i would expect.

So i'm wondering if visual studio is allowing something which is forbidden
by the standard, or is there a way to force a default conversion operator
to be called when an object instance is passed to an ellipsis?


It's just undefined behavior, so it may appear to work in some situations.

In general, ... arguments are evil, and the printf() family of functions
shouldn't be used without great need. Get down with operator<< and
<iostream>, and you can do nearly all your formatting needs. All more
typesafe than .... Your ultimate problem was VC++ did not emit a warning
when it saw %s not matched with a char *. Some compilers can warn like
that, as a custom extension.

(If you can figure out the iomanipulators!)

--
Phlip
Jun 22 '06 #6
Andrey Tarasevich wrote:
...
Your class type is clearly not a POD, which means that the behavior is undefined.
...


Oops.... I kind of assumed that your class in not a POD, but I don't
really know that. Even though have a conversion operator in your class,
it still can be a POD, meaning that the behavior might still be defined
as far as we are talking about supplying arguments for '...' parameters.

However, even if your type is a POD, it still will be passed as a class
object (as a whole, that is). No conversion to 'char*' type will be
performed by the compiler. That would immediately mean that the behavior
is still undefined, but for a different reason: using '%s' format
specifier with a non-string argument.

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #7

"Kufa" <po*****@gmail.com> skrev i meddelandet
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi,

I have a class String which implements the const char*() operator. I
was doing some tests today and did something like:
String test( "hello world" );
printf( "%s\n", test ); // instead of test.c_str(); or (const
char*)test

This code compiled and worked fine on a x86/visual studio 7.1. On
some
other platforms, gcc was giving errors, as i would expect.

So i'm wondering if visual studio is allowing something which is
forbidden by the standard, or is there a way to force a default
conversion operator to be called when an object instance is passed
to
an ellipsis?


You are not allowed to pass a class object thru an ellipsis. If you
do, the compiler is allowed to do whatever it likes.
The CString class in VS7.1 does its trick by having the class object
contain a char* pointer only. That way printf cannot tell the
difference between a raw pointer and a class containing the pointer!
Bo Persson
Jun 22 '06 #8
Andrey Tarasevich wrote:
Andrey Tarasevich wrote:
...
Your class type is clearly not a POD, which means that the behavior
is undefined. ...
Oops.... I kind of assumed that your class in not a POD, but I don't
really know that. Even though have a conversion operator in your
class, it still can be a POD,


With a user-defined c-tor?
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '06 #9
Victor Bazarov wrote:
Andrey Tarasevich wrote:
Andrey Tarasevich wrote:
...
Your class type is clearly not a POD, which means that the behavior
is undefined. ...


Oops.... I kind of assumed that your class in not a POD, but I don't
really know that. Even though have a conversion operator in your
class, it still can be a POD,


With a user-defined c-tor?
...


Oh, OK, thanks, I missed that part. In that case my first message was
correct. Although there are lots of valuable stuff in the second one as
well :O)

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #10
In article <4g*************@individual.net>, bo*@gmb.dk
says...

[ ... ]
You are not allowed to pass a class object thru an ellipsis. If you
do, the compiler is allowed to do whatever it likes.


This is true only for non-POD class objects. For POD
objects, the behavior is well defined.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 23 '06 #11
Thanks for all the answers!

Jun 24 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Perkins | last post by:
Is there a reason why the ... notation as a literal for Ellipsis is only allowed inside a slice? Would allowing it elsewhere frighten the parser? Chris Perkins
6
by: Jonas Kölker | last post by:
so, I've read the manuals, done a few hacks, read the quick reference. however, one thing still eludes me: what (the hell) is Ellipsis? what's it good for? how do I use it; how does the...
2
by: Mike - EMAIL IGNORED | last post by:
Consider: #include <syslog.h> // Open syslog in constructor. void MyClass:: MyMemberFunction(...) {
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
15
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: ...
2
by: cmk128 | last post by:
Hi The following code have a compile error: printf_format.cpp: In function `void print(int, char*, ...)': printf_format.cpp:6: invalid conversion from `const char*' to `int' printf_format.cpp:6:...
2
by: marss | last post by:
IE allows to show text that exceeds the container's boundaries as unfinished (by adding ellipsis). e.g.: <div style="text-overflow:ellipsis;overflow:hidden;width:20px">some long text</div>...
3
by: femina | last post by:
heres a sample program #include<iostream> using namespace std; void add(...) { cout<<"match using ellipsis"; } void add(int a,int b) { cout<<"a and b are"<<a<<b;
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
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...

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.