C++:如何检查模板函数中使用的数据类型?

New*_*bie 0 c++ templates visual-studio-2008

我需要为每种类型使用不同的函数,但大多数代码保持不变.

如何测试我收到的模板函数参数中的数据类型(或结构)?

Mat*_*lia 6

如何测试我收到的模板函数参数中的数据类型(或结构)?

你为什么要?如果您想使用模板,那是因为每种类型的代码都相同.否则你只需使用常规的重载函数.

另一方面,如果您只有一部分算法需要特定于类型,则不会停止使用从模板主函数调用的重载子函数:

template <typename T>
bool IsFooBar(T Baz)
{
    // here the code is common for every type
    // ...
    // Here Baz must be modified in a type-speficic way:
    DoQuux(Baz);
    // ...
    return /* ... */;
}

// DoQuux is a regular overloaded function
void DoQuux(int & Baz)
{
    // ...
}

void DoQuux(double & Baz)
{
    // ...
}

void DoQuux(std::string & Baz)
{
    // ...
}

// ... other DoQuux ...
Run Code Online (Sandbox Code Playgroud)