- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我遇到了一段在 GCC 和 MSVC 中表现不同的代码:
#include <utility>
typedef int IType;
template<typename> struct A;
template<int... Ns>
struct A<std::integer_sequence<IType, Ns...>> {
using type = bool;
};
using B = typename A<std::make_integer_sequence<IType, 3>>::type;
int main() {
B b;
}
这很高兴在两个编译器上编译。但是,如果您将 IType 定义为
typedef long IType;
MSVC 仍然有效,而 GCC 说:
source>:12:61: error: invalid use of incomplete type 'struct A<std::integer_sequence<long int, 0, 1, 2> >'
12 | using B = typename A<std::make_integer_sequence<IType, 3>>::type;
| ^~~~
<source>:5:27: note: declaration of 'struct A<std::integer_sequence<long int, 0, 1, 2> >'
5 | template<typename> struct A;
| ^
<source>: In function 'int main()':
<source>:15:3: error: 'B' was not declared in this scope
15 | B b;
| ^
Compiler returned: 1
因此,显然当 IType 很长时,GCC 无法使用 A 的第二个更专业的定义,因此失败。我真的很难理解为什么
int
和
long
在这里被 GCC 区别对待。
最佳答案
template< class T, T... Ints >
class integer_sequence;
也就是说,值的类型是
T
.
IType
至
long
,也是
Ns...
的类型应改为
long...
:
typedef int IType;
template<typename> struct A;
template<IType... Ns> // <--- HERE
struct A<std::integer_sequence<IType, Ns...>> {
using type = bool;
};
否则,您将获得特化
struct A<long, int, int, int>
哪个不匹配
struct A<long, long, long, long>
(MSVC 似乎对此更宽容,但 GCC 行为在 IMO 上更正确)。
关于c++ - 具有 integer_sequence 的专用模板。海湾合作委员会与 MSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62634760/
我使用下面给出的代码实现了编译时检查以检查是否对某些内容进行了排序: template struct is_sorted { static constexpr bool value = tru
如下直接使用默认参数值生成整数序列会导致硬错误(编译器clang-3.6): #include #include #include template // say M - arity, N -
我有这段代码可以生成 1 到 10 的编译时数组 template // when called below, Is will be 0 - N constexpr std::array make_
我有一个看起来像这样的函数: template std::ostream& vector_insert(std::ostream& lhs, const char* delim, const T&
我想用一个integer_sequence来判断一个范围内的数字是否都在某个值以下:is_range()会返回true,否则返回false,如下所示: #include #include using
如果我理论上有一个整数序列,比如 std::integer_sequence 我如何使用一些编译时谓词过滤它以获得可能更小的 std::integer_sequence ? 为了论证,假设我只想要偶数
给定: typedef std::integer_sequence allowed_args_t; 和: template void foo() { static_assert( /*fire
#include #include using namespace std; template auto map_filter_tuple(F f, T &t) { return mak
我想知道是否有办法转换 std::array成索引序列? constexpr std::array x = {0, 3, 4, 5, 8}; constexpr auto integer_sequen
我基本上使用这个问题作为引用起草了编译时主要检查: Compile time prime checking 我有一个 IsPrime::value 可用。 我想设计一个 find 元函数,它在编译时基
出于教育目的,我尝试创建一个 std::integer_sequence并将其元素汇总为参数包。我希望这很简单,并在下面编写了代码。 第 1 步:创建一组 add() 操作以正确处理同类的、基于整数的
我想找到一个值在 std::integer_sequence 中第一次出现的位置。 标准库中是否有用于此任务的算法? 如果没有,什么是做这件事的好方法? -- 下面是我的尝试。它有效,但我觉得它不是很
如何将 std::integer_sequence 作为模板参数传递给元函数(即不是函数模板)? 给出例如以下用例(但不限于此): 我想使用整数序列从参数包中删除最后的 N 类型。我想我可以使用 th
我为符合以下规则的枚举创建了一个可迭代生成器: 枚举是一个整数序列,没有间隙 给定枚举的最后一个元素不是实际的枚举元素 这个类看起来像这样: template class EnumArrayNonS
我正在尝试创建一个可以用作...的函数 foobar() int main() { auto x = foobar(__func__); // decltype(x) = std::i
所以,我遇到了一段在 GCC 和 MSVC 中表现不同的代码: #include typedef int IType; template struct A; template struct A> {
这个问题在这里已经有了答案: template parameter packs access Nth type and Nth element (5 个回答) 2年前关闭。 我想知道如何访问 std:
考虑一个例子: #include template struct pack { static constexpr std::size_t size = sizeof...(Ts); }; t
也就是说,给定 constexpr std::array{1,2}将它传递给会输出类型 std::integer_sequence 的函数或辅助类? 从类型世界跳转到“constexpr value”
有时我想反转 index_sequence 中的值并使用结果反转某些类似元组的值,就像这个反转中的值的插图编译时的 constexpr std::array。 #include #include
我是一名优秀的程序员,十分优秀!