I am sketching a small generic type-wrapper-template in C++14, that is intended to enable, disable, or extend the underlying type's interface using mixins.
Here is the code for this wrapper (stripped down a little):
namespace detail {
/// Helper template that is forwarded to the mixins used
/// for the extension of wrapper<•> in order to enable
/// access to the actual type `Derived`
template <typename Derived>
struct Cast {
using type = Derived;
template <typename T>
static constexpr Derived& self(T* self) { return *static_cast<Derived*>(self); }
template <typename T>
static constexpr Derived const& self(T const* self) { return *static_cast<Derived const*>(self); }
};
/// This helper template is used to derive from all the Mixins
/// one after another, making sure the constructor is mixed in as well:
template <typename Cast, typename T,template <typename...> class...Mixins>
struct wrapper_impl;
template <typename Cast, typename T,template <typename...> class First, template <typename...> class...Rest>
struct wrapper_impl<Cast, T, First, Rest...>
: First<Cast, T>
, wrapper_impl<Cast, T, Rest...>
{
using First<Cast, T>::First;
using wrapper_impl<Cast, T, Rest...>::wrapper_impl;
};
template <typename Cast, typename T, template <typename...> class First>
struct wrapper_impl<Cast, T, First>
: First<Cast, T>
, wrapper_impl<Cast, T>
{
using First<Cast, T>::First;
using wrapper_impl<Cast, T>::wrapper_impl;
};
template <typename Cast, typename T>
struct wrapper_impl<Cast, T> {
};
}
template <typename T, typename Tag, template <typename...> class...Mixins>
class wrapper : public detail::wrapper_impl<detail::Cast<wrapper<T,Tag,Mixins...>>, T, Mixins...> {
public:
using value_type = T;
using detail::wrapper_impl<detail::Cast<wrapper<T,Tag,Mixins...>>, T, Mixins...>::wrapper_impl;
T& get() { return *reinterpret_cast<T*>(&m_buffer); }
T const& get() const { return *reinterpret_cast<T const*>(&m_buffer); }
template <typename...Args>
void construct(Args&&...args) {
new (&m_buffer) T(std::forward<Args>(args)...);
}
void destruct() {
get().~T();
}
~wrapper() {
destruct();
}
private:
std::aligned_storage_t<sizeof(T), alignof(T)> m_buffer;;
};
Now, I can mix in a construction form some type T using the following mixin-template:
template <typename T>
struct constructor_from {
template <typename Cast, typename U>
struct mixin {
explicit mixin(T const& value) {
Cast::self(this).construct(U{value});
}
};
};
So far this works out fine, e.g. I can wrap an int like so:
using my_int = wrapper<int, struct Tag, constructor_from<int>::mixin>;
my_int instance{42};
See a the full code here on godbolt. However, I want to disable unwanted implicit conversions, which is why I marked the constructor of constructor_from::mixin as explicit. But unexpectedly, the following seemingly still compiles without errors on clang-5.0.0 (see on compiler-explorer):
using my_int = wrapper<int, struct Tag, constructor_from<int>::mixin>;
my_int instance{4.2};
Questions:
- Is this a bug in clang-5.0.0? It seemingly compiles with the expected failure in gcc-7.3.
- If it's not a bug, am I doing something unreasonable (undefined, unspecified, ...) here that explains why it should compile despite the
explicitconstructor on clang-5.0.0? Or maybe I'm misunderstanding how constructor inheritance works?
Edit:
As @someprogrammerdude has pointer out, I may be misunderstanding the explicit construction. However, I still don't understand why the above code should compile, while this does not:
struct foo {
explicit foo(int) {}
};
struct bar : foo {
using foo::foo;
};
int main() {
bar instance{4.2};
}
It would be great if someone could point me to the corresponding sections in the c++ standard that allow the construction in the case of my wrapper, but prevent it in the case here