仅适用于基本 POD 的模板专业化
模板专业化是否有一个微妙的技巧,以便我可以将一种专业化应用于基本 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";}
};
或者我是否必须为每个内置类型编写专门化?
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";}
};
单元测试。
int main()
{
DoStuff<int> intStuff;
intStuff(); // Print POD Type
DoStuff<std::string> strStuff;
strStuff(); // Print Generic
}
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)).
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实只想要基本类型而不是用户定义的 POD 类型,那么以下方法应该可行:
如果您还想要用户定义的 POD 类型,则使用
boost::is_pod
而不是>non_void_fundamental<>
(如果您使用 C++11 并出于优化目的执行此操作,请使用std::is_trivially_copyable<>
代替)。If you really want only fundamental types and not user-defined POD types then the following should work:
If you also want user-defined POD types, then use
boost::is_pod<>
instead ofnon_void_fundamental<>
(and if you're using C++11 and doing this for optimization purposes, usestd::is_trivially_copyable<>
instead).在 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
这样广泛的假设,而是微调您的约束以匹配您的方法真正需要的内容。In C++11, many traits have been added to the standard library, and most seem particularly aimed toward interesting specializations (and notably bitwise manipulations).
The top-level trait you could be interested in is
std::is_trivial
, however there are many others:std::is_trivially_default_constructible
std::is_trivially_copy_constructible
std::is_trivially_move_constructible
std::is_trivially_copyable
(can be copied viamemcpy
)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.Boost 具有
提升: :is_pod
。这就是您要找的吗?(我从未使用过它,所以我不会因为尝试制定示例所需的精确代码而让自己感到尴尬。)
Boost has
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.)