Detecting the datatype of argument

Hi All - Question from a newbie. I have a print() function that I would like to overload. So, I would like it to work in both scenarios below:

print(string my_string);
print(float ** my_matrix);

How do I detect/identify inside the function, the data type of the argument passed in, especially when it's a pointer to pointer (as in float ** my_matrix)? Once the function knows the data type, it can manage the printing accordingly.

Thanks.
closed account (3qX21hU5)
Just create 2 functions one that accepts a pointer to a pointer of a float as a parameter and one that accepts a string. The compiler will then figure out which one to use by what you pass in as a argument to the function.

For example (I left out the pointer to a pointer stuff so you can figure it out).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void print(const std::string& myString)
{
	std::cout << myString << std::endl;
}

void print(float myFloat)
{
	std::cout << myFloat << std::endl;
}

int main()
{
	print("Hello");   // Will call the string version
	print(1.05);       // Will call the float version
}
Last edited on
Thanks Zereo! Isn't that too much code? Is there way to identify the data type? And probably just use a single function?
Isn't that too much code?

That's the way it's done in C++.

And probably just use a single function?

You would still have to pass in another argument indicating the type of the argument to be printed.
Last edited on
skafetaur wrote:
And probably just use a single function?
Template it.
1
2
3
4
5
template<typename T>
void print(T const &rhs)
{
    std::cout << rhs << std::endl;
}

print will work for every data type that cout has an operator<< overload for.

EDIT:
But if you're overloading operator<<, you don't really need another print function.
skafetaur wrote:
Once the function knows the data type, it can manage the printing accordingly.
This is what function overloading was intended for (what Zereo explained in his post)
Last edited on
Thank you Zereo & Thumper.

Leaving aside all of the above, is there way in C++ to detect the data type of an variable (forget functions and overloading), especially a pointer-to-pointer? For instance I could detect an integer or float data type by checking if the variable value is unchanged in upper & lower case (a string containing alpha numeric characters would fail that test).

How do I check if a variable is of type ** (pointer-to-pointer)?

Thanks once again.
> is there way in C++ to detect the data type of an variable
go to its declaration, look at the left
skafetaur wrote:
How do I check if a variable is of type ** (pointer-to-pointer)?
C++11 introduces decltype and type_traits. (this is a good article: http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html)

Consider the following example:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>
#include <type_traits>

int main()
{
    int **someDoublePointer;
    std::cout << std::boolalpha 
              << std::is_same<decltype(someDoublePointer), int**>::value
              << std::endl;

}
true


There are a whole bunch of additional functions in type_traits as well. It's worth checking out:
http://www.cplusplus.com/reference/type_traits/

ne555 wrote:
go to its declaration, look at the left

ne555 is right though. There should very rarely be a scenario in C++ where you don't already know the exact type of data you're dealing with.
Last edited on
Thanks again.
Topic archived. No new replies allowed.