 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ed Avis Guest
|
Posted: Fri Jun 27, 2003 2:09 pm Post subject: Re: turn off optimizations for to_string |
|
|
[email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) writes:
| Quote: | template< class T
std::string to_string( const T & val) {
std::ostringstream out;
out << val;
return out.str();
}
What I want is the above NOT to be inline, no matter what compiler
flags are on.
|
The compiler cannot inline if it has not seen the function
definition. So remove the function body from the header file (or make
your own copy of the header file and change it). Then put the
function body in a new .cpp file, compile that and link the resulting
object file with the rest of your program.
Choose compiler flags so that your modified
included before the system standard one.
--
Ed Avis <ed (AT) membled (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Jun 27, 2003 6:12 pm Post subject: Re: turn off optimizations for to_string |
|
|
"John Torjo" <jtorjo (AT) yahoo (DOT) com> wrote in message
| Quote: | template< class T
std::string to_string( const T & val) {
std::ostringstream out;
out << val;
return out.str();
}
I'm willing to trade as much speed as possible, and am looking
(hopefully) for a pretty portable solution - hopefully not compiler
dependent.
|
In the header file util.hpp,
template< class T>
const std::string to_string( const T & val);
In the cpp export file util.exp,
export
template< class T>
const std::string to_string( const T & val) {
std::ostringstream out;
out << val;
return out.str();
}
The linker is supposed to magically find the exported definitions at link
time. But no compiler actually does this. So make a file instantiate.cpp
that includes util.exp and instantiates the versions you need.
As a bonus, doing things this way makes sure that you don't accidentally
create zillioins of instantiations. In the example we only support int,
double, const char *, and link time errors will let us know this.
// instantiate.cpp
#include "util.exp"
static void junk() {
to_string((int)(0));
to_string((double)(0));
to_string((const char *)(0));
}
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Sat Jun 28, 2003 12:43 pm Post subject: Re: turn off optimizations for to_string |
|
|
Ed Avis <ed (AT) membled (DOT) com> wrote
| Quote: | jtorjo (AT) yahoo (DOT) com (John Torjo) writes:
template< class T
std::string to_string( const T & val) {
std::ostringstream out;
out << val;
return out.str();
}
What I want is the above NOT to be inline, no matter what compiler
flags are on.
The compiler cannot inline if it has not seen the function
definition. So remove the function body from the header file
.... |
Since this is template function is is not so easy to get rid of
function body (until export).
Maybe something like this:
extern std::string to_string_impl(ostream& (*)(ostream&, X&), const
X&);
// assumes existing standalone function operator<<() for both
// primitive types and user defined types
template
str::string to_string(const T& val) {
typedef ostream& (*put_to_stream_t)(ostream&, T&);
put_to_stream_t put = &(operator<<);
return to_string_impl((X&)val, (ostream& (*)(ostream&, X&))put);
}
This hack works if all references are implemented in the same way and
requires casting but shouldn't take more than few instructions in
code.
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Torjo Guest
|
Posted: Mon Jun 30, 2003 5:39 pm Post subject: Re: turn off optimizations for to_string |
|
|
[email]pavel_vozenilek (AT) yahoo (DOT) co.uk[/email] (Pavel Vozenilek) wrote in message
news:<731020ca.0306271114.19b24471 (AT) posting (DOT) google.com>...
| Quote: | Ed Avis <ed (AT) membled (DOT) com> wrote
[email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) writes:
template< class T
std::string to_string( const T & val) {
std::ostringstream out;
out << val;
return out.str();
}
What I want is the above NOT to be inline, no matter what compiler
flags are on.
The compiler cannot inline if it has not seen the function
definition. So remove the function body from the header file
...
Since this is template function is is not so easy to get rid of
function body (until export).
Maybe something like this:
extern std::string to_string_impl(ostream& (*)(ostream&, X&), const
X&);
// assumes existing standalone function operator<<() for both
// primitive types and user defined types
template
str::string to_string(const T& val) {
typedef ostream& (*put_to_stream_t)(ostream&, T&);
put_to_stream_t put = &(operator<<);
return to_string_impl((X&)val, (ostream& (*)(ostream&, X&))put);
}
This hack works if all references are implemented in the same way and
requires casting but shouldn't take more than few instructions in
code.
|
Sounds quite interesting. I need to check it (I don't have a compiler
at hand now), but it might work.
I think it might also turn off inlining of operator<<, which is not
what I want.
Now, I was thinking of another way to do this (not tested it yet
though):
// having the code I originally showed
extern std::string print_val( to_string_impl_base * p);
template< class T>
std::string to_string( const T & val) {
to_string_impl< T> impl(val);
to_string_impl_base * p = &impl;
return print_val( p + 1);
}
// in a .cpp file
std::string print_val( to_string_imp_base * p) {
p[ -1].to_string();
}
Hopefully it would turn off optimizations Have to try it
Best,
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Wed Jul 02, 2003 2:51 am Post subject: Re: turn off optimizations for to_string |
|
|
[email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote in message
news:<c638aac5.0306300407.1cceb8c2 (AT) posting (DOT) google.com>...
[snip]
| Quote: |
extern std::string to_string_impl(ostream& (*)(ostream&, X&), const
X&);
// assumes existing standalone function operator<<() for both
// primitive types and user defined types
template
str::string to_string(const T& val) {
typedef ostream& (*put_to_stream_t)(ostream&, T&);
put_to_stream_t put = &(operator<<);
return to_string_impl((X&)val, (ostream& (*)(ostream&, X&))put);
}
Sounds quite interesting. I need to check it (I don't have a compiler
at hand now), but it might work.
I think it might also turn off inlining of operator<<, which is not
what I want.
Yes it will but you may offer some helper function instead: |
template
void ostream_write_wrapper(ostream& o, const T& val)
{
o << val;
}
template
std::string to_string(const T& val) {
typedef void (*put_to_stream_t)(ostream&, T&);
put_to_stream_t put = &ostream_write_wrapper;
...
}
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|