c++ - std::array constructor inheritance -



c++ - std::array constructor inheritance -

i'm trying extended variant of std::array math vectors (and expose same interface array without boilerplate code). know std::valarray want fixed size proper typing in matrix multiplications. array fits perfectly. when seek inherit constructor fails.

struct vec2d : std::array<float, 2> { using array::array; }; // simplified struct vec : std::vector<float> { using vector::vector; }; std::array<float, 2> x = {1, 2}; vec y = {1, 2}; vec2d z = {1, 2}; // error: not convert ‘{1, 2}’ // ‘<brace-enclosed initializer list>’ ‘vec2d’

this error reported gcc 4.8.2 , clang 3.4. lastly says vec2d have implicit default/copy/move constructors. yes, array have implicit ctor in contrary vector have ctor initializer_list. since ctors inherited natural inherit possibility initialize in same way array initialized.

question: why have error instead of expected behavior (similar array initialization)?

note: i can write forwarding manually create work, doesn't elegant ctor inheritance.

struct vec2d : std::array<float, 2> { using array::array; // nasty boilerplate code don't want have in c++11 template <typename... args> vec2d(args &&... args) : array({float(std::forward<args>(args))...}) {} };

std::array designed aggregate, intentionally not define constructors.

unfortunately, means it's not possible inherit , same behaviour, aggregates cannot have base of operations classes.

why need inherit std::array anyway? plan add together private members? if not, build framework around free functions operating on std::array, or perhaps typedef it.

if want inherit std::array, you'll have take losing aggregate status , provide constructors want yourself.

c++ c++11

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -