5

我正在用 C++14 绘制一个小型通用类型包装模板,它旨在使用 mixins 启用、禁用或扩展底层类型的接口。

这是这个包装器的代码(精简了一点):

namespace detail {
    /// Helper template that is forwarded to the mixins used 
    /// for the extension of wrapper<•&gt; 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;;
};

T现在,我可以使用以下 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});
        }
    };
};

到目前为止,这很好,例如我可以int这样包装:

using my_int = wrapper<int, struct Tag, constructor_from<int>::mixin>;
my_int instance{42};

在godbolt上查看完整代码。但是,我想禁用不需要的隐式转换,这就是我将构造函数标记为constructor_from::mixinas的原因explicit。但出乎意料的是,以下似乎仍然在 clang-5.0.0 上编译没有错误(参见compiler-explorer):

using my_int = wrapper<int, struct Tag, constructor_from<int>::mixin>;
my_int instance{4.2};

问题:

  • 这是 clang-5.0.0 中的错误吗?它似乎与gcc-7.3中的预期失败一起编译。
  • 如果它不是错误,我是否在这里做了一些不合理的事情(未定义,未指定,...),这解释了为什么尽管explicit在 clang-5.0.0 上有构造函数,它还是应该编译?或者我可能误解了构造函数继承的工作原理?

编辑:

正如@someprogrammerdude 指出的那样,我可能误解了这个explicit结构。但是,我仍然不明白为什么上面的代码应该编译,而没有:

struct foo {
    explicit foo(int) {}
};

struct bar : foo {
    using foo::foo;
};

int main() {
    bar instance{4.2};
}

如果有人能指出我在 c++ 标准中允许在 my 的情况下进行构造的相应部分,那就太好了wrapper,但在这种情况下阻止它

4

0 回答 0