模板专业化仅适用于基本POD
Template Specialization for basic POD only
模板专门化有没有一个微妙的技巧,这样我就可以对basic POD
应用一个专门化(当我说基本POD时,我并不特别想要结构POD(但我会接受))。
template<typename T>
struct DoStuff
{
void operator()() { std::cout << "Genericn";}
};
template<>
struct DoStuff</*SOme Magic*/>
{
void operator()() { std::cout << "POD Typen";}
};
或者我必须为每种内置类型编写专门化?
template<typename T>
struct DoStuff
{
void operator()() { std::cout << "Genericn";}
};
// 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 Typen";}
};
单元测试。
int main()
{
DoStuff<int> intStuff;
intStuff(); // Print POD Type
DoStuff<std::string> strStuff;
strStuff(); // Print Generic
}
如果您真的只想要基本类型而不想要用户定义的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 << "Genericn"; } const
};
template<>
struct DoStuff<T, true>
{
void operator ()() { std::cout << "POD Typen"; } const
};
如果您还想要用户定义的POD类型,请使用boost::is_pod<>
而不是non_void_fundamental<>
(如果您使用C++11并出于优化目的这样做,请改用std::is_trivially_copyable<>
)。
在C++11中,许多特性已添加到标准库中,并且大多数特性似乎特别针对有趣的专业化(尤其是逐位操作)。
你可能感兴趣的顶级特征是std::is_trivial
,但还有很多其他特征:
std::is_trivially_default_constructible
std::is_trivially_copy_constructible
std::is_trivially_move_constructible
std::is_trivially_copyable
(可通过memcpy
复制)
一般来说,标准已经尝试获得尽可能细粒度的特征,因此您不需要依赖is_pod
这样广泛的假设,而是对约束进行微调,以匹配您的方法真正需要的内容。
Boost具有boost::is_pod
。这就是你要找的吗?
(我从未使用过它,所以我不会因为试图为您的示例制定所需的精确代码而让自己感到尴尬。)
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 模板函数仅适用于VS
- 如何在cmake中包含适用于g++或viceversa的库
- 适用于win32、linux、mac的POSIX C包装器
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"
- 从uint8_t到NPY_UINT16 PyArray_SimpleNewFromData.适用于Linux,但不适用于