0

Consider the following code.

int id = 666;
stringstream stream(stringstream::in | stringstream::out);
stream << "Object " << id << " active.";
file.write(stream.str());

It combines all the values preceded by << in a string quite nicely. I would love to discover a shorter, easier to use version with less code duplication. Furthermore, the above code is just an example, and the command should accept arbitrary combinations of variables and strings. Ideally something like:

int id = 666;
WRITE("Object ", id, " active.");

Is this possible in C++ in a portable way, even with Boost.Preprocessor, inlined functions and all the bag of tricks.

5
  • I think you need to be more specific, since as it stands you should just be able to just wrap the original code in a function called WRITE. Aug 28, 2012 at 8:50
  • Could you make your WRITE something like printf with the ... concept?
    – MartyE
    Aug 28, 2012 at 8:55
  • 1
    So you just want to put an arbitrary number of different type values out to the file? You could try a variadic template. Aug 28, 2012 at 8:57
  • I appologise for the very unclear question. I tried to improve it, clarifying that I need a variable number of arguments and that it is not obligatory to use stringstream.
    – Vorac
    Aug 28, 2012 at 9:49
  • 1
    A std::stringstream is open for reading and writing by default, there's no need to construct it with (stringstream::in | stringstream::out) Aug 28, 2012 at 9:50

3 Answers 3

3

You can accomplish this without type checking using a macro:

//filewrite.h
#define WRITE(first, second, third) \
{\
   stringstream stream(stringstream::in | stringstream::out);\
   stream << first << second << third;\
   file.write(stream.str());\
}

Or, cleaner, with a template function:

template<typename T1, typename T2, typename T3>
void WRITE(T1 const& first, T2 const& second, T3 const& third, fstream& file)
{
   stringstream stream(stringstream::in | stringstream::out);
   stream << first << second << third;
   file.write(stream.str());
}
5
  • given the title, I think the intent is do it w/o typechecking, hence not specific types in your function params
    – MartyE
    Aug 28, 2012 at 8:56
  • 1
    Now any way to extend it for taking unlimited number of requirements?
    – MartyE
    Aug 28, 2012 at 9:00
  • 2
    @MartyE you could, using varargs, but I don't have time to check for the syntax right now. Aug 28, 2012 at 9:01
  • 1
    When using a macro, unlimited parameters could also be used easily if the (x << y << z) notation can be used in the user code.
    – stefaanv
    Aug 28, 2012 at 9:11
  • The last two comments could very well be the two correct answers to my query. I'll check them out!
    – Vorac
    Aug 28, 2012 at 9:51
1

If you really don't want type-checking don't use C++, it's a statically-typed language!

If you just mean you want it to work for any type, either use a macro (eurgh) or use variadic templates, something like https://gitlab.com/redistd/redistd/blob/master/include/redi/printers.h which supports:

#include <redi/printers.h>
using redi::println;
int main()
{
  int id = 666;
  println("Object ", id, " active.");  // write arguments to stdout
}

The println function takes any number of arguments and was shamelessly stolen from inspired by some example code from Howard Hinnant.

It would be quite easy to adapt that to write to an fstream instead of std::cout e.g. by adding

inline
void
fprintln()
{ file << std::endl; }

template<typename T0, typename... T>
  inline
  void
  fprintln(const T0& t0, const T&... t)
  {
    print_one(file, t0);
    fprintln(t...);
  }

Then:

 fprintln("Object ", id, " active.");  // write arguments to 'file'
1

You don't need (nor want) macros. This is what templates were designed for:

template <typename T>
void
write( std::string const& prefix, T const& value, std::string const& suffix )
{
    std::ostringstream fmt;
    fmt << prefix << value << suffix;
    file.write( fmt.str() );
}

On the other hand, why bother? Why not just let the client code use the idiomatic:

file << prefix << value << suffix;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.