How to declare a function argument that can accept any type in C++

4 Answers

0 votes
#include <iostream>
#include <any>
 
void AcceptAnyType(const std::any& x) {
    if (x.type() == typeid(int)) {
        std::cout << "int\n";
    } else if (x.type() == typeid(double)) {
        std::cout << "double\n";
    } else if (x.type() == typeid(std::string)) {
        std::cout << "string\n";    
    } else if (x.type() == typeid(char)) {
        std::cout << "char\n";   
    } else if (x.type() == typeid(bool)) {
        std::cout << "bool\n";
    } else {
        std::cout << "Unknown type: " << x.type().name() << '\n';
    }
}
 
int main() {
    AcceptAnyType(9001);
    AcceptAnyType(3.14);
    AcceptAnyType('A');
    AcceptAnyType(std::string("ABCD"));
    AcceptAnyType(true);
}

      
      
/*
run:
      
int
double
char
string
bool

*/

 



answered Jul 31, 2025 by avibootz
edited Jul 31, 2025 by avibootz
0 votes
#include <iostream>
#include <typeinfo>
 
template<typename T>
void AcceptAnyType(const T& x) {
    std::cout << "Type is: " << typeid(x).name() << '\n';
}
 
int main() {
    AcceptAnyType(9001);
    AcceptAnyType(3.14);
    AcceptAnyType('A');
    AcceptAnyType("XYZ");
    AcceptAnyType(std::string("ABCD"));
    AcceptAnyType(true);
}

      
      
/*
run:
      
Type is: i
Type is: d
Type is: c
Type is: A4_c
Type is: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Type is: b

*/

 



answered Jul 31, 2025 by avibootz
edited Jul 31, 2025 by avibootz
0 votes
#include <iostream>
#include <string>
#include <any>

void AcceptAnyType(const std::any& x) {
    if (x.type() == typeid(int)) {
        std::cout << "int\n";
    } else if (x.type() == typeid(double)) {
        std::cout << "double\n";
    } else if (x.type() == typeid(std::string)) {
        std::cout << "string\n";    
    } else if (x.type() == typeid(char)) {
        std::cout << "char\n";   
    } else if (x.type() == typeid(const char*)) {
        std::cout << "const char *\n";   
    } else if (x.type() == typeid(bool)) {
        std::cout << "bool\n";
    } else {
        std::cout << "Unknown type: " << x.type().name() << '\n';
    }
}

int main() {
    AcceptAnyType(std::any(9001));
    AcceptAnyType(std::any(3.14));
    AcceptAnyType(std::any('A'));
    AcceptAnyType(std::any("XYZ")); // const char* literal
    AcceptAnyType(std::any(std::string("ABCD")));
    AcceptAnyType(std::any(true));
}



/*
run:
      
int
double
char
const char *
string
bool

*/



 



answered Jul 31, 2025 by avibootz
0 votes
#include <iostream>
#include <variant>

using MyVariant = std::variant<int, double, std::string>;

void AcceptAnyType(const MyVariant& x) {
    std::visit([](auto&& val) {
        std::cout << "Value is: " << val << '\n';
    }, x);
}

int main() {
    AcceptAnyType(9001);
    AcceptAnyType(3.14);
    AcceptAnyType('a');
    AcceptAnyType("XYZ");
    AcceptAnyType(std::string("ABCD"));
    AcceptAnyType(true);
}

      
      
/*
run:
      
Value is: 9001
Value is: 3.14
Value is: 97
Value is: XYZ
Value is: ABCD
Value is: 1

*/

 



answered Jul 31, 2025 by avibootz
...