带有包扩展的alignas在Visual Studio 2019中不起作用



我正在为Variant类编写一个简单的实现,但当我尝试实例化AlignedStorage:类型的变量时,Visual Studio拒绝了这段代码

using std::size_t;
template <size_t SIZE, size_t... ALIGNMENT>
struct AlignedStorage
{
struct Type
{
alignas(ALIGNMENT...) unsigned char storage[SIZE];
};
};
template <size_t SIZE, size_t... ALIGNMENT>
using AlignedStorageT = typename AlignedStorage<SIZE, ALIGNMENT...>::Type;
/**** variant storage class ****/
template <typename... Types>
class VariantStorage
{
public:
void *GetStorage() { return mStorage.storage; }
const void *GetStorage() const { return mStorage.storage; }
template <typename T>
T &GetStorageAs() { return *reinterpret_cast<T*>(GetStorage()); }
template <typename T>
const T &GetStorageAs() const { return *reinterpret_cast<const T*>(GetStorage()); }
unsigned GetDiscriminator() const { return mDiscriminator; }
void SetDiscriminator(unsigned discriminator) { mDiscriminator = discriminator; }
private:
using LargestT = typename LargestType<Typelist<Types...>>::Type;
AlignedStorageT<sizeof(LargestT), alignof(Types)...> mStorage;  ///////// Visual studio ERROR (not GCC)
unsigned int mDiscriminator;  // discriminated union tag
};

它似乎与ALIGNMENT包扩展有问题,Gcc似乎工作得很好,但它拒绝了另一条线(这似乎与VC++很好(:

template <typename T, typename V>  // CRTP
class VariantChoice;
template <typename...>
class Variant;
template <typename Type, typename... Types>  
class VariantChoice<Type, Variant<Types...>>
{
friend class Variant<Types...>;  // private ctor
protected:  
static constexpr unsigned int mDiscriminatorIndex = IndexOfType<Type, Typelist<Types...>>::Value;  // index of Type in typelist
private:
VariantChoice() = default;
~VariantChoice() = default;
VariantChoice(const VariantChoice &other);
VariantChoice(VariantChoice &&other);
VariantChoice &operator=(const VariantChoice &other);
VariantChoice &operator=(VariantChoice &&other);
using Derived = Variant<Types...>;    // CRTP 
Derived &AsDerived() { return static_cast<Derived&>(*this); }
const Derived &AsDerived() const { return static_cast<Derived const&>(*this); }
};
/**** variant class ****/
template <typename... Types>
class Variant : VariantStorage<Types...>, VariantChoice<Types, Variant<Types...>>...
{
template <typename, typename>
friend class VariantChoice;  // enable CRTP (private inheritance)
public:
using VariantChoice<Types, Variant>::VariantChoice...;  // inherited ctors
Variant() {}
Variant(const Variant &other);
Variant(Variant &&other);

template <template <typename...> class Variant_, typename... Types_>
Variant(Variant_<Types_...> &&other);

using VariantChoice<Types, Variant>::operator=...;     ///////// GCC ERROR: bring copy/move assignment operators in scope doesn't compile ???? (VC++ works)
Variant &operator=(Variant const &other);
Variant &operator=(Variant &&other);
private:
}; 

这个代码正确吗?还是我在做一些不规范的事情?

编辑我更正了alignof(类型(。。。

对于使用VariantChoice<Types, Variant>::operator=...不能与gcc一起使用的问题,您应该将gcc至少更新为gcc10,我将ubuntu与gcc9.3一起使用,也遇到了同样的问题。当我升级到gcc12时,一切都很好。

最新更新