Skip to content

Commit

Permalink
Add alignment to variant
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Feb 5, 2018
1 parent 16ebe49 commit 2e2d0cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
23 changes: 14 additions & 9 deletions jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5193,19 +5193,19 @@ namespace variant_detail {

constexpr static int NO_TYPE = -1;

template<size_t Size, typename ... Args>
template<size_t Size, size_t Align, typename ... Args>
class VariantImpl;

template<size_t Size>
class VariantImpl<Size> {
template<size_t Size, size_t Align>
class VariantImpl<Size, Align> {
public:
VariantImpl() {
type_ = NO_TYPE;
}

private:
alignas(Align) char data_[Size];
int type_;
char data_[Size];

protected:
int& type() { return type_; }
Expand Down Expand Up @@ -5243,13 +5243,18 @@ class VariantImpl<Size> {
void assign() {}
};

template<size_t Size, typename T, typename ... Args>
class VariantImpl<Size, T, Args...> : public VariantImpl<
template<size_t Size, size_t Align, typename T, typename ... Args>
class VariantImpl<Size, Align, T, Args...> : public VariantImpl<
(sizeof(T) > Size ? sizeof(T) : Size),
(alignof(T) > Align ? alignof(T) : Align),
Args...
>
{
using Base = VariantImpl<(sizeof(T) > Size ? sizeof(T) : Size), Args...>;
using Base = VariantImpl<
(sizeof(T) > Size ? sizeof(T) : Size),
(alignof(T) > Align ? alignof(T) : Align),
Args...
>;

constexpr static int MY_ID = sizeof...(Args);

Expand Down Expand Up @@ -5331,8 +5336,8 @@ class VariantImpl<Size, T, Args...> : public VariantImpl<
};

template<typename ... Args>
class Variant : public VariantImpl<0, Args...> {
using Base = VariantImpl<0, Args...>;
class Variant : public VariantImpl<0, 1, Args...> {
using Base = VariantImpl<0, 1, Args...>;

public:
Variant() { }
Expand Down
23 changes: 14 additions & 9 deletions variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ namespace variant_detail {

constexpr static int NO_TYPE = -1;

template<size_t Size, typename ... Args>
template<size_t Size, size_t Align, typename ... Args>
class VariantImpl;

template<size_t Size>
class VariantImpl<Size> {
template<size_t Size, size_t Align>
class VariantImpl<Size, Align> {
public:
VariantImpl() {
type_ = NO_TYPE;
}

private:
alignas(Align) char data_[Size];
int type_;
char data_[Size];

protected:
int& type() { return type_; }
Expand Down Expand Up @@ -63,13 +63,18 @@ class VariantImpl<Size> {
void assign() {}
};

template<size_t Size, typename T, typename ... Args>
class VariantImpl<Size, T, Args...> : public VariantImpl<
template<size_t Size, size_t Align, typename T, typename ... Args>
class VariantImpl<Size, Align, T, Args...> : public VariantImpl<
(sizeof(T) > Size ? sizeof(T) : Size),
(alignof(T) > Align ? alignof(T) : Align),
Args...
>
{
using Base = VariantImpl<(sizeof(T) > Size ? sizeof(T) : Size), Args...>;
using Base = VariantImpl<
(sizeof(T) > Size ? sizeof(T) : Size),
(alignof(T) > Align ? alignof(T) : Align),
Args...
>;

constexpr static int MY_ID = sizeof...(Args);

Expand Down Expand Up @@ -151,8 +156,8 @@ class VariantImpl<Size, T, Args...> : public VariantImpl<
};

template<typename ... Args>
class Variant : public VariantImpl<0, Args...> {
using Base = VariantImpl<0, Args...>;
class Variant : public VariantImpl<0, 1, Args...> {
using Base = VariantImpl<0, 1, Args...>;

public:
Variant() { }
Expand Down

0 comments on commit 2e2d0cf

Please sign in to comment.