确定 C++ 中的类型是否为同一基础类型的别名

发布于 2024-10-11 14:26:50 字数 323 浏览 2 评论 0原文

我想编写一个模板化函数,它根据传入的模板类类型来更改其行为。为此,我想确定传入的类型。例如,这样的事情:

template <class T>
void foo() {
  if (T == int) { // Sadly, this sort of comparison doesn't work
    printf("Template parameter was int\n");
  } else if (T == char) {
    printf("Template parameter was char\n");
  }
}

这可能吗?

I'd like to write a templated function which changes its behavior depending on template class types passed in. To do this, I'd like to determine the type passed in. For example, something like this:

template <class T>
void foo() {
  if (T == int) { // Sadly, this sort of comparison doesn't work
    printf("Template parameter was int\n");
  } else if (T == char) {
    printf("Template parameter was char\n");
  }
}

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挖鼻大婶 2024-10-18 14:26:50

这就是模板专业化的目的,搜索该术语会给出大量示例。

#include <iostream>

template <typename T>
void foo() 
{ 
    std::cout << "Unknown type " << typeid(T).name() << "\n";
}

template<typename T>
void fooT(T const& x) { foo<T>(); }

template<>
void foo<int>()
{    printf("Template parameter was int\n");
}

template<>
void foo<char>()
{    printf("Template parameter was char\n");
}

int main()
{
    fooT(std::cout);
    fooT(5);
    fooT('a');
    fooT("Look Here");
}

This is the purpose of template specialization, a search for that term gives tons of examples.

#include <iostream>

template <typename T>
void foo() 
{ 
    std::cout << "Unknown type " << typeid(T).name() << "\n";
}

template<typename T>
void fooT(T const& x) { foo<T>(); }

template<>
void foo<int>()
{    printf("Template parameter was int\n");
}

template<>
void foo<char>()
{    printf("Template parameter was char\n");
}

int main()
{
    fooT(std::cout);
    fooT(5);
    fooT('a');
    fooT("Look Here");
}
白鸥掠海 2024-10-18 14:26:50

通过使用部分专业化的力量,这可以在编译时完成:

template<class T, class U>
struct is_same_type
{
    static const bool value = false;
};
template<class T>
struct is_same_type<T, T>
{
    static const bool value = true;
};

template <class T>
void foo() 
{
    if (is_same_type<T, int>::value) 
    {
        printf("Template parameter was int\n");
    }
    else if (is_same_type<T, char>::value) 
    {
        printf("Template parameter was char\n");
    }
}

在我的脑海中编译,但仍然应该工作。

By using the power of partial specialization, this can be done at compile time:

template<class T, class U>
struct is_same_type
{
    static const bool value = false;
};
template<class T>
struct is_same_type<T, T>
{
    static const bool value = true;
};

template <class T>
void foo() 
{
    if (is_same_type<T, int>::value) 
    {
        printf("Template parameter was int\n");
    }
    else if (is_same_type<T, char>::value) 
    {
        printf("Template parameter was char\n");
    }
}

Compiled in my head, but should work nonetheless.

本宫微胖 2024-10-18 14:26:50

使用模板专业化或 typeid 可能适合您,尽管您可能更喜欢模板专业化,因为它不会产生 typeid 的运行时成本。例如:

#include <iostream>
#include <typeinfo>

template <typename T>
void foo(T arg) {
  if (typeid(arg) == typeid(int)) std::cout << "foo<T> where T is int\n";
  else if (typeid(arg) == typeid(double)) std::cout << "foo<T> where T is double\n";
  else if (typeid(arg) == typeid(char)) std::cout << "foo<T> where T is char\n";
}

template <>
void foo<int>(int arg) {
  std::cout << "foo<int>\n";
}

int main() {
  foo(3);   // foo<int>
  foo(3.0); // foo<T> where T is double
  foo('c'); // foo<T> where T is char
}

Using template specialization or typeid would probably work for you, although you might prefer template specialization as it won't incur the runtime cost of typeid. For example:

#include <iostream>
#include <typeinfo>

template <typename T>
void foo(T arg) {
  if (typeid(arg) == typeid(int)) std::cout << "foo<T> where T is int\n";
  else if (typeid(arg) == typeid(double)) std::cout << "foo<T> where T is double\n";
  else if (typeid(arg) == typeid(char)) std::cout << "foo<T> where T is char\n";
}

template <>
void foo<int>(int arg) {
  std::cout << "foo<int>\n";
}

int main() {
  foo(3);   // foo<int>
  foo(3.0); // foo<T> where T is double
  foo('c'); // foo<T> where T is char
}
噩梦成真你也成魔 2024-10-18 14:26:50

直接使用 type_info ,或者更好的是 typeid 运算符来做到这一点。

#include <typeinfo>
template < typename T > 
T max( T arg1, T arg2 ) {
   cout << typeid( T ).name() << "s compared." << endl;
   return ( arg1 > arg2 ? arg1 : arg2 );
}

Use type_info directly, or better still typeid operator to do that.

#include <typeinfo>
template < typename T > 
T max( T arg1, T arg2 ) {
   cout << typeid( T ).name() << "s compared." << endl;
   return ( arg1 > arg2 ? arg1 : arg2 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文