Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9061
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: concatenate variable number of arguments and types to a char * |
| Date | 2011-08-19 06:26 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <j2koks$7f3$1@dont-email.me> (permalink) |
| References | <14393cec-d1a9-4971-8a27-120e87b045a0@glegroupsg2000goo.googlegroups.com> |
On 19.08.2011 06:04, suresh wrote:
> Hi
> How to write a function which would concatenate variable number of arguments of
> different type and return a char *. For example I need the function
>
> f("his weight is = ", 51.5, "pounds");
>
> to return the string "his weight is = 51.5 pounds".
One simple way is like (off the cuff)
template< class T >
string toString( T const& v )
{
stringstream stream_;
stream_ << v;
return stream.str();
}
struct S
{
string s_;
public:
template< class T >
S& operator<<( T const& v )
{
s_ += toString( v );
return *this;
}
operator char const* () const
{
return s_.c_str();
}
};
It can be elaborated and, not the least, optimized.
Anyway, then, when you want to call a function
void foo( char const* s )
you can say things like
foo( S() << "his weight is = " << 51.5 << "pounds" )
Cheers & hth.,
- Alf
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
concatenate variable number of arguments and types to a char * suresh <suresh.amritapuri@gmail.com> - 2011-08-18 21:04 -0700
Re: concatenate variable number of arguments and types to a char * "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-19 06:26 +0200
Re: concatenate variable number of arguments and types to a char * Felix Bytow <felix.bytow@informatik.tu-chemnitz.de> - 2011-08-19 06:50 +0200
Re: concatenate variable number of arguments and types to a char * "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-19 08:04 +0200
Re: concatenate variable number of arguments and types to a char * PaulR <pchristor@yahoo.co.uk> - 2011-08-19 04:01 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 14:00 +0100
Re: concatenate variable number of arguments and types to a char * Juha Nieminen <nospam@thanks.invalid> - 2011-08-19 14:31 +0000
Re: concatenate variable number of arguments and types to a char * "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-19 16:57 +0200
Re: concatenate variable number of arguments and types to a char * Juha Nieminen <nospam@thanks.invalid> - 2011-08-19 19:15 +0000
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 16:33 +0100
Re: concatenate variable number of arguments and types to a char * Juha Nieminen <nospam@thanks.invalid> - 2011-08-19 19:18 +0000
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 20:29 +0100
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 14:18 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 22:37 +0100
Re: concatenate variable number of arguments and types to a char * Ian Collins <ian-news@hotmail.com> - 2011-08-20 09:43 +1200
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 15:08 -0700
Re: concatenate variable number of arguments and types to a char * Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-19 16:14 -0400
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 13:54 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 22:01 +0100
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 14:20 -0700
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 14:21 -0700
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 11:03 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 19:23 +0100
Re: concatenate variable number of arguments and types to a char * red floyd <no.spam.here@its.invalid> - 2011-08-19 11:13 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 19:39 +0100
csiph-web