简体   繁体   English

仅适用于基本 POD 的模板专业化

[英]Template Specialization for basic POD only

Is there a subtle trick for template specialization so that I can apply one specialization to basic POD (when I say basic POD I don't particularly want struct POD (but I will take that)).模板专业化是否有一个微妙的技巧,以便我可以将一个专业化应用于basic POD (当我说基本 POD 时,我并不特别想要 struct POD(但我会接受))。

template<typename T>
struct DoStuff
{
    void operator()() { std::cout << "Generic\n";}
};
template<>
struct DoStuff</*SOme Magic*/>
{
    void operator()() { std::cout << "POD Type\n";}
};

Or do I have to write specializations for each of the built in types?或者我是否必须为每个内置类型编写专业化?

template<typename T>
struct DoStuff
{
    void operator()() { std::cout << "Generic\n";}
};


// Repeat the following template for each of
// unsigned long long, unsigned long, unsigned int, unsigned short, unsigned char
//          long long,          long,          int,          short, signed   char
// long double, double, float, bool
// Did I forget anything?
//
// Is char covered by unsigned/signed char or do I need a specialization for that?
template<>  
struct DoStuff<int>
{
    void operator()() { std::cout << "POD Type\n";}
};

Unit Test.单元测试。

int main()
{
    DoStuff<int>           intStuff;
    intStuff();            // Print POD Type


    DoStuff<std::string>   strStuff;
    strStuff();            // Print Generic
}

If you really want only fundamental types and not user-defined POD types then the following should work:如果你真的只想要基本类型而不是用户定义的 POD 类型,那么以下应该有效:

#include <iostream>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_fundamental.hpp>
#include <boost/type_traits/is_same.hpp>

template<typename T>
struct non_void_fundamental : boost::integral_constant<
    bool,
    boost::is_fundamental<T>::value && !boost::is_same<T, void>::value
>
{ };

template<typename T, bool Enable = non_void_fundamental<T>::value>
struct DoStuff
{
    void operator ()() { std::cout << "Generic\n"; } const
};

template<>
struct DoStuff<T, true>
{
    void operator ()() { std::cout << "POD Type\n"; } const
};

If you also want user-defined POD types, then use boost::is_pod<> instead of non_void_fundamental<> (and if you're using C++11 and doing this for optimization purposes, use std::is_trivially_copyable<> instead).如果您还需要用户定义的 POD 类型,则使用boost::is_pod<>而不是non_void_fundamental<> (如果您使用 C++11 并出于优化目的这样做,请改用std::is_trivially_copyable<> ) .

In C++11, many traits have been added to the standard library, and most seem particularly aimed toward interesting specializations (and notably bitwise manipulations).在 C++11 中,标准库中添加了许多特征,并且大多数特征似乎特别针对有趣的专业化(尤其是按位操作)。

The top-level trait you could be interested in is std::is_trivial , however there are many others:您可能感兴趣的顶级特征是std::is_trivial ,但还有许多其他特征:

  • std::is_trivially_default_constructible
  • std::is_trivially_copy_constructible
  • std::is_trivially_move_constructible
  • std::is_trivially_copyable (can be copied via memcpy ) std::is_trivially_copyable (可以通过memcpy复制)

In general, the Standard has tried to get as finer grained traits as possible so you need not rely on such broad assumptions as is_pod but instead fine-tune your constraints to match what your methods really need.一般来说,标准试图获得尽可能细粒度的特征,因此您不必依赖于is_pod类的广泛假设,而是微调您的约束以匹配您的方法真正需要的内容。

Boost has boost::is_pod . Boost 有boost::is_pod Is that what you're looking for?这就是你要找的吗?

(I've never used it, so I won't embarrass myself by trying to formulate the precise code that you require for your example.) (我从来没有用过它,所以我不会试图为你的例子制定你需要的精确代码来让自己难堪。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM