0

I'm creating an equivalent to JavaScript's console.log in C++, but am unsure how to have my function expect different types of arguments.

In JavaScript:

function print(arg)
{
    if(typeof arg=="number") { ... }
    if(typeof arg=="string") { ... }       
}

Of course, JavaScript doesn't care what you give a function, but C++ does, so how can I have it catch any ( or at least specify types for it to accept ), to be handled later in the function itself?

All I have so far:

void print(string input)
{
    cout << input << "\n";
}
  • do you want it to be able to accept different types of input such as an int or double or string? – R Nar Oct 19 '15 at 16:01

3 Answers3

4

You can accomplish this with a function template.

template <typename T>
void print(const T& output)
{
    std::cout << output << "\n";
}

This will create a print function for each type you pass to it.

Edit:

From the comments if you want this to work with arrays as well then you can add

template<typename T, std::size_t N>
void print(T (&output)[N])
{
    for (std::size_t i = 0; i < N; i++)
    {
        std::cout << output[i] << " ";
    }
    std::cout << "\n";
}

You cann see all of this working together in this Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

This is a solved problem.

std::cerr << "My console output with a number! " << 42 << std::endl;

This goes to stderr, an output stream typically handled by your shell differently than stdout, so as to aid in debugging and fault-finding. It's the perfect analogue to JavaScript's console.log, and it already exists.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

You can use function templates as described by NathanOliver.

You can also use function overloading: just define multiple functions with the same name but different argument types. The compiler will choose the right one. Function overloading might be better than a template function depending on what you are doing. In particular, if every type of parameter requires a different function body to handle it, function overloading probably makes more sense than a template.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • 2
    That _is_ Nathan's solution ... all your variant does is require that the OP write each overload out manually. – Lightness Races in Orbit Oct 19 '15 at 16:10
  • I tried to add more justification to my answer. Templates are a way to generate multiple functions that all share the same body. Function overloading is a way to write functions with different bodies that can be called similarly. Depending on what you're doing, you might want to have different bodies, so function overloading makes more sense. I'm not sure if template specialization even works for function templates; so if you wanted to have different function bodies without using overloading it could be hard. – David Grayson Oct 19 '15 at 16:43
  • Function templates, when instantiated, produce function overloads. Those overloads absolutely don't "share the same body" — they differ wherever you've used a dependent name. You're trying to compare apples and apples, in a sense. The only difference is that Nathan gets his apples from the greengrocer, whereas you're growing each variety yourself in your garden. – Lightness Races in Orbit Oct 19 '15 at 17:02
  • Ok, that makes sense. I had a slightly different definition of "body" than you, I was thinking of the characters in written by the programmer in he source file. Sometimes you want the bodies of the function overloads to be very different, so you should grow them in your own garden. Like, if an integer is passed in, then you want to format it in some special way. And if a string is passed in, then you want to print "Here is a string" or something. – David Grayson Oct 19 '15 at 18:01
  • @Da​​​​​​​​​​​​vid: Template specialisations may also differ greatly, for the same reason. There is really no difference here. – Lightness Races in Orbit Oct 19 '15 at 18:03
  • 1
    Actually, Overloading is very much what I needed, I need the actual code to be different depending on the argument, so this is what I need :-) – jeremiah chandler Oct 19 '15 at 21:13