【发布时间】:2017-12-07 02:27:04
【问题描述】:
所以我试图在我的一个项目中实现小对象优化,但我遇到了一个奇怪的编译器错误。以下是重现该问题的一些简化代码:
#include <type_traits>
template<typename T>
class Wrapper {
T thing;
public:
Wrapper(T&& thing) {
// ...
}
};
class Test {
static const size_t PADDING_SIZE = 64;
public:
template<
typename T,
std::enable_if_t<sizeof(Wrapper<std::decay_t<T>>) <= PADDING_SIZE, int> = 0
// Error on this line ^
>
Test(T&& thing) {
new (padding) Wrapper<std::decay_t<T>>(std::forward<T>(thing));
}
char padding[PADDING_SIZE];
};
int main() {
auto t = Test(0.0f);
}
基本上,我需要获取一个任意对象,将其放入包装器中,并在填充中实例化包装器的一个实例,但我需要为可以适合填充的类型使用一个包装器,并为适合填充的类型使用不同的包装器太大的类型(其中一个包装器将对象存储在适当的位置,而另一个为其分配外部空间)。显然我想支持完美转发。
不幸的是,VS2017 给了我以下编译器错误:error C2027: use of undefined type 'Wrapper<decay<_Ty>::type>'。我可以用Wrapper<T> 而不是Wrapper<std::decay_t<T>> 编译它就好了,但我认为我需要使用衰变类型。 Clang 按原样编译它。
那么这里的问题是什么?我有点卡住了。
【问题讨论】:
标签: c++ templates compiler-errors