Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Variadic template issue

JimR:

I have the following code I'm trying to adapt for my own purposes. This is a learning exercise for me to attempt an update of my C++ skills. This was originally written with Clang 3.1 in mind as far as I can tell.

I've tried compiling with Clang versions from 3.1 to 4.0 and GCC 4.9 to 7.1 with very similar results.


These are the error messages from GCC 5.1

Error 1: In instantiation of 'constexpr Struct::Struct(T&& ...) [with T = {Struct, bar >&}; Fields = {foo, bar}]':
:46:12: required from here

Error 2: :28:61: error: mismatched argument pack lengths while expanding 'Fields'
constexpr Struct(T &&...x) : Fields{static_cast(x)}... {}

Please ELI5 if you have the patience :P

You can see this in godbolts compiler doohickey here: http://ift.tt/2sOkXSd

EDIT:

Given the answers by @Passer-By and @Daniel-Jour, I wonder if Struct(Struct const &) = default; is even necessary. Will removing this constructor from Struct have effects of which I am not aware or do no foresee (I am no C++ Swami!)?

Is the constructor constexpr Struct(T &&...x) : Fields{static_cast(x)}... {} a good stand-in for what would otherwise be generated by Struct(Struct const &) = default;?

I'm feeling pretty ambiguous about either proposed solution so far.

End EDIT


// From http://ift.tt/1K1yRnS
// edited a bit to stop the compiler whining about comments in multiline macros
#include
#include

#define SELFAWARE_IDENTIFIER(NAME) \
template \
struct NAME { \
T NAME; /* field name */ \
constexpr static char const *name() { return #NAME; } /* field type */ \
using type = T; /* field value generic accessor */ \
T &value() & { return this->NAME; } \
T const &value() const & { return this->NAME; } \
T &&value() && { return this->NAME; } \
};

template
struct Struct : Fields... {
// A convenience alias for subclasses
using struct_type = Struct;

// Preserve default constructors
Struct() = default;
Struct(Struct const &) = default;

// Forwarding elementwise constructor
template
constexpr Struct(T &&...x) : Fields{static_cast(x)}... {} // Error 2 here
};

SELFAWARE_IDENTIFIER(foo)
SELFAWARE_IDENTIFIER(bar)
// Aliasing a Struct instance
using FooBar = Struct, bar >;
// Inheriting a Struct instance (requires inheriting constructors)
struct FooBar2 : Struct, bar> { using struct_type::struct_type; };

static_assert(std::is_trivial::value, "should be trivial");
static_assert(FooBar{2, 3.0}.foo + FooBar{2, 4.0}.foo == 4, "2 + 2 == 4");


FooBar frob(int x) {
FooBar f = {x, 0.0};
f.foo += 1;
f.bar += 1.0;
return f; // Error 1 here
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Variadic template issue

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×