问题标签 [typetraits]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
279 浏览

c++ - isAbstract 模板和视觉工作室

以下模板将决定 T 是否是 g++ 的抽象。

例如:struct myClass2 { virtual void f() {} }; 结构 myClass1 { 虚拟 void f() = 0; };

但是,它在 Visual Studio 9.0 中失败并出现以下错误:

有谁知道问题是什么以及如何解决这个问题?

MSDN报告说他们现在有一个is_abstract自 VS2008 以来的类作为 TR1 的一部分(在 header 内type_traits)。但是,我的安装中似乎缺少它。

PS。由于冗长而无聊的原因,我无法通过 Boost 重新实现它。

更新

另外,尝试更换,

与每一个,

不走运——所有人都说不能为 U 推导出模板参数。

0 投票
4 回答
278 浏览

c++ - How can I use templates to determine the appropriate argument passing method?

As I understand it, when passing an object to a function that's larger than a register, it's preferable to pass it as a (const) reference, e.g.:

This avoids having to perform a potentially expensive copy of the argument.

However, when passing a type that fits into a register, passing it as a (const) reference is at best redundant, and at worst slower:

My problem is, I'd like to know how to get the best of both worlds when I'm using a templated class that needs to pass around either type:

Is there a template decision method that allows me to pick the correct type? Something that would let me do,

that would pick the better method depending on the type?


Edit:

After a fair amount of timed tests, the difference between the two calling times is different, but very small. The solution is probably a dubious micro-optimization for my situation. Still, TMP is an interesting mental exercise.

0 投票
3 回答
1406 浏览

c++ - 模板类函数 T:如何判断 T 是否为指针?

作为这个问题的后续行动:我需要在这样的类函数中做出决定:

T 是否为指针。

在上面引用的问题中,答案是使用部分模板专业化。据我发现,这对于类函数是不可能的。这是真的?如果是这样,是否有另一种方法可以找出 T 是否是指针?

0 投票
9 回答
4506 浏览

c++ - 如何编写 `is_complete` 模板?

回答完这个问题后,我试图is_complete在 Boost 库中查找模板,但我意识到 Boost.TypeTraits 中没有这样的模板。为什么Boost库中没有这样的模板?它应该是什么样子?

上面的代码是不正确的,因为应用于sizeof不完整类型是非法的。什么是好的解决方案?在这种情况下是否可以以某种方式应用 SFINAE?


好吧,如果不违反ODR 规则,一般无法解决这个问题,但是有一个特定于平台的解决方案对我有用。

0 投票
5 回答
2472 浏览

c++ - 通用对象载体类 - C++

我需要创建一个通用的对象载体类。我想出了一些简单的东西

T有默认构造函数(无参数)时,这很有效。T当有参数化的构造函数时,事情变得复杂了。所以我重写了这个类

item_将变量更改为T*并使用 的复制构造函数创建了一个新实例T。这再次运行良好,直到T是指针类型。我的意思是ObjectCarrier<Foo*>不会工作。

我想知道如何设计这个类,以便它适用于几乎所有类型的类型。我想我可能需要创建一个traits专门用于指针的类型。但不幸的是,我无法完成这项工作。

任何帮助都会很棒。

0 投票
2 回答
1148 浏览

c++ - 类型特征定义。特征 blob 和元函数

阅读一些源代码,我发现了下一个特征定义:

这对我来说看起来很奇怪。我会将所有特征分组到一个独特的类中,如下所示:

你们中的任何人都可以弄清楚为什么第二种方法可能比第一种方法更脆弱,或者更难添加新特征吗?

0 投票
3 回答
395 浏览

c++ - 帮助类型特征

假设我们有以下模板类

我们不能改变 Wrap。这很重要。

假设有派生自 的类Wrap<T>。例如,

我们也不能改变这些类。以上所有课程都是第 3 方。他们不是我的。

我需要以下编译时间type_traits

我需要什么?

0 投票
1 回答
26797 浏览

c++ - C++ 类型特征来检查类是否有运算符/成员

可能重复:
是否可以编写 C++ 模板来检查函数是否存在?

是否可以使用 boost 类型特征或其他机制来检查特定模板参数是否具有运算符/函数,例如std::vector模板参数有operator[],而std::pair没有。

0 投票
5 回答
27259 浏览

c++ - `is_base_of` 是如何工作的?

以下代码如何工作?

  1. 请注意,这B是私人基地。这是如何运作的?

  2. 注意operator B*()是常量。它为什么如此重要?

  3. 为什么template<typename T> static yes check(D*, T);优于static yes check(B*, int);

注意:它是boost::is_base_of. 这适用于广泛的编译器。

0 投票
1 回答
1633 浏览

c++ - 整数特征(is_integer,is_integral)

我需要关于整数的两个特征。

  1. 第一个类似于std::is_integral(or boost::is_integral),但可用于用户定义的类型(例如,包装 an 的类int,例如int_wrapper):如果该类型的行为类似于整数并且其表示类似于标准整数类型(例如sizeof(T) * CHAR_BITS == std::numeric_limits<T>::digits,如果T是无符号的) ,则为 true整数类型的定义非常严格,因为它由这些类型的列表组成。所以专业化std::is_integral似乎很困难,如果不被禁止的话(尽管我认为没有明确说明):is_integral是“主要”类型特征(20.7.4.1,注 3:对于类型 T,恰好一个主要类型特征是正确的,在我的情况下int_wrapper已经is_class等于为真)。如果我专门研究这个特征,我会承担什么风险int_wrapper?你知道适合我需要的特质类(例如在 Boost 中)吗?

  2. 我需要的第二个特征是具有整数语义的类型(使用位算术运算、位操作等)。例如mpz_classfrom GMP 将满足此特征。std::numeric_limits<T>::is_integer适合这个特质吗?我读到可以专门化和设置numeric_limits<T>::is_integer == trueifT行为像一个整数,而且(在 C++ 标准中)术语“整数”和“整数”是同义词(在这种情况下,我们总是有numeric_limits<T>::is_integer == is_integral<T>::value

总之,我是更好地为我的精确需求定义自己的特征,还是尝试扩展标准的特征?