Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9064
| 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 08:04 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <j2kud3$t4$1@dont-email.me> (permalink) |
| References | <14393cec-d1a9-4971-8a27-120e87b045a0@glegroupsg2000goo.googlegroups.com> <j2kq3j$3bg$1@bruford.hrz.tu-chemnitz.de> |
On 19.08.2011 06:50, Felix Bytow wrote:
> Am 19.08.2011 06:04, schrieb suresh:
>> 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".
>>
>> thanks
>> suresh
>
> Hi,
> this might do, what you want:
>
> #include<string>
> #include<sstream>
> #include<iostream>
>
> template<typename Head>
> std::string concat(Head const& head)
> {
> std::stringstream strm;
> strm<< head;
> return strm.str();
> }
>
> template<typename Head, typename... Tail>
> std::string concat(Head const& head, Tail... tail)
> {
> return (concat(head) + concat(tail...));
> }
>
> int main(void)
> {
> std::cout<< concat("The answer is ", 42, "!")<< std::endl;
> return 0;
> }
>
> Ofcourse this code makes use of variadic templates which are part of
> C++11. You will find that not all compilers support that already.
>
> Using the old tools you will not be able to write a function that fits
> your needs completely...
The C++98 solution I posted is more efficient, O(n) versus yours O(n^2),
at the cost of slightly different notation.
The C++0x machinery you're using can be used to wrap such a more
efficient solution in order to get conventional usage notation.
However I think the C++98 solution is OK as it is... ;-)
Cheers & hth.,
- Alf
PS: Please replace tab characters with spaces before posting. Many
editors do that with just a few keypresses.
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