MSVC 无法编译 SFINAE 检查
MSVC fails to compile SFINAE checks
是否有一些我应该注意的编译器标志?根据 GCC 和 Clang 但不是 MSVC,以下代码工作得很好并且符合预期。是什么让 std::allocator
的分配在这里如此不同?
#include <type_traits>
#include <memory>
template <typename T, typename = std::void_t<>>
constexpr bool declares_allocate = false;
template <typename T>
constexpr bool declares_allocate<T, std::void_t<decltype(&T::allocate)>> = true;
struct normal_struct {
auto allocate() -> void {}
};
template <typename T>
struct struct_template {
auto allocate() -> void {}
};
template <typename T>
class class_template_public {
public:
auto allocate() -> void {}
};
template <typename T>
class class_template_private {
private:
auto allocate() -> void {}
};
auto main() -> int {
auto allocator = std::allocator<float>();
auto memory = allocator.allocate(1024);
static_assert(declares_allocate<normal_struct>); // pass
static_assert(declares_allocate<struct_template<float>>); // pass
static_assert(declares_allocate<class_template_public<float>>); // pass
// static_assert(declares_allocate<class_template_private<float>>); // fails
static_assert(declares_allocate<std::allocator<float>>); // fails when compiled by MSVC but not Clang or GCC
allocator.deallocate(memory, 1024);
return 0;
}
根据 C++ 标准实现,std::allocator<T>::allocate
可以定义为:
T* allocate(size_t n, const void* hint = 0);
T* allocate(size_t n);
T* allocate(size_t n, const void* hint); // deprecating usage of hint
T* allocate(size_t n);
也就是第二种实现把allocate
变成了一个重载的成员函数。而那个目前用于 MSVC's standard library:
_NODISCARD __declspec(allocator) _Ty* allocate(_CRT_GUARDOVERFLOW const size_t _Count) {
return static_cast<_Ty*>(_Allocate<_New_alignof<_Ty>>(_Get_size_of_n<sizeof(_Ty)>(_Count)));
}
_CXX17_DEPRECATE_OLD_ALLOCATOR_MEMBERS _NODISCARD __declspec(allocator) _Ty* allocate(
_CRT_GUARDOVERFLOW const size_t _Count, const void*) {
return allocate(_Count);
}
这样的实现使得 &T::allocate
模棱两可,因此在替换过程中被拒绝。
是否有一些我应该注意的编译器标志?根据 GCC 和 Clang 但不是 MSVC,以下代码工作得很好并且符合预期。是什么让 std::allocator
的分配在这里如此不同?
#include <type_traits>
#include <memory>
template <typename T, typename = std::void_t<>>
constexpr bool declares_allocate = false;
template <typename T>
constexpr bool declares_allocate<T, std::void_t<decltype(&T::allocate)>> = true;
struct normal_struct {
auto allocate() -> void {}
};
template <typename T>
struct struct_template {
auto allocate() -> void {}
};
template <typename T>
class class_template_public {
public:
auto allocate() -> void {}
};
template <typename T>
class class_template_private {
private:
auto allocate() -> void {}
};
auto main() -> int {
auto allocator = std::allocator<float>();
auto memory = allocator.allocate(1024);
static_assert(declares_allocate<normal_struct>); // pass
static_assert(declares_allocate<struct_template<float>>); // pass
static_assert(declares_allocate<class_template_public<float>>); // pass
// static_assert(declares_allocate<class_template_private<float>>); // fails
static_assert(declares_allocate<std::allocator<float>>); // fails when compiled by MSVC but not Clang or GCC
allocator.deallocate(memory, 1024);
return 0;
}
根据 C++ 标准实现,std::allocator<T>::allocate
可以定义为:
T* allocate(size_t n, const void* hint = 0);
T* allocate(size_t n);
T* allocate(size_t n, const void* hint); // deprecating usage of hint
T* allocate(size_t n);
也就是第二种实现把allocate
变成了一个重载的成员函数。而那个目前用于 MSVC's standard library:
_NODISCARD __declspec(allocator) _Ty* allocate(_CRT_GUARDOVERFLOW const size_t _Count) {
return static_cast<_Ty*>(_Allocate<_New_alignof<_Ty>>(_Get_size_of_n<sizeof(_Ty)>(_Count)));
}
_CXX17_DEPRECATE_OLD_ALLOCATOR_MEMBERS _NODISCARD __declspec(allocator) _Ty* allocate(
_CRT_GUARDOVERFLOW const size_t _Count, const void*) {
return allocate(_Count);
}
这样的实现使得 &T::allocate
模棱两可,因此在替换过程中被拒绝。