[英]How to receive a function parameter of any type in C++ and get the type of the passed variable inside the function?
我希望能夠接收函數參數的任何類型,並在函數內部確定給定的類型並相應地采取行動。
偽代碼:
void myFunction(any argument)
{
if(argument is int)
{
myFunctionInt(argument);
}
if(argument is string)
{
myFunctionString(argument);
}
}
謝謝。
首先,你應該看看簡單的函數重載,它允許你在oldschool fassion中實現它:
void my_function(int value) {
std::cout << "int\n";
}
void my_function(std::string value) {
std::cout << "string\n";
}
雖然這有一些缺點,因為它需要相當多的代碼行(每種類型的新函數!)。 您還需要專門命名類型(使用所有限定符),這可能是不合需要的。
要使用pseudecode,在使用C ++ 17時,可以使用if-constexpr
在編譯時選擇正確的分支。
這很棒,因為在std::string
分支內部甚至可以讓你說像value.length()
! 對於常規的舊if
這是不可能的。
那么如何確定什么類型的value
? std::is_same_v<T, U>
允許執行比較,只有在這種情況下T
恰好是U
- int
或std::string
時才產生true
:
#include <type_traits>
#include <string>
#include <iostream>
template<typename T>
void my_function(T value) {
if constexpr (std::is_same_v<T, int>) {
std::cout << "int\n";
}
else if constexpr (std::is_same_v<T, std::string>) {
std::cout << "string\n";
}
}
這就是它的表現:
my_function(5); //-> prints out 'int'
my_function(std::string{ "hello" }); //-> prints out 'string'
但是, std::is_same_v
不考慮限定符。 這是一個問題,例如這不會打印出任何東西:
my_function<const int>(5);
因為const int
與int
。 要擺脫const
和任何引用&
std::decay_t
派上用場:
template<typename T>
void my_function(T value) {
if constexpr (std::is_same_v<std::decay_t<T>, int>) {
std::cout << "int\n";
}
else if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
std::cout << "string\n";
}
}
這是正確的,如現在所見:
my_function<const int>(5);
my_function<const std::string&>(std::string{ "hello" });
int string
那么在cpp中有一個很酷的東西叫做模板函數
例如
template <typename T>
void tempFunction(T param){...}
對於檢查類型的問題,可以使用type_traits庫中的is_same來比較類型
例如
#include <iostream>
#include <type_traits>
template <typename T>
void tempFunction(T param) {
if (std::is_same<T, std::string>::value)
std::cout << "string" << std::endl;
else if (std::is_same<T, int>::value)
std::cout << "int" << std::endl;
//...
}
這就是你如何使用它
int main(void) {
int a = 10;
std::string str = "Moo";
std::string str2 = "Moo";
tempFunction(str);
tempFunction(a);
tempFunction(str2);
return 0;
}
預期產量:
string
int
string
模板功能並為隱性類型提供重載。
template <typename T>
void myFunction(T a){
myFunction(a);
}
void myFunction(int i)
{
}
void myFunction(std::string)
{
}
你可以有一個模板
template <typename T>
void myFunction(T param);
並專門針對某些類型
template <>
void myFunction<int>(int param)
{
// int things here
}
template <>
void myFunction<std::string>(std::string param)
{
// string things here
}
模板是一個很大的選擇。
https://docs.microsoft.com/en-us/cpp/extensions/generics-and-templates-visual-cpp?view=vs-2019
#include <iostream>
#include <typeinfo>
using namespace std;
template<typename T>
void myFunction(T argument) {
cout << typeid(argument).name() << endl;
if (typeid(argument) == typeid(char)) {
cout << "some stuff on char" << endl;
}else if (typeid(argument) == typeid(int)) {
cout << "some stuff on int" << endl;
}else if (typeid(argument) == typeid(const char*)) {
cout << "some stuff on string" << endl;
}
}
int main()
{
myFunction('s');
myFunction(10);
myFunction("smoker hehe");
cout << "Great example by deon cagadoes\n";
system("pause");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.