如何继承不适用于基础 class 实例的模板构造函数?

How to inherit template constructor that will not work with base class instances?

我有 许多 从基础派生的 classes。这些 classes 必须从 base 继承构造函数,但该构造函数应该只适用于派生或 base class 实例。

基础 class 示例:

template<typename T, typename U>
struct bar
{ 
  bar() = default;

  template<typename _Bar_or_Derived>
  bar(const _Bar_or_Derived &); // must accept any bar or its derived classes
};

派生 classes 示例:

template<typename T, typename U>
struct foo : public bar<T, U>
{ 
  using bar<T, U>::bar; 

 // must inherit something like foo(const Foo_or_Bar&)
};

template<typename T, typename U>
struct not_foo : public bar<T, U>
{ 
  using bar<T, U>::bar; 

 // must inherit something like not_foo(const NotFoo_or_Bar&)
};

这种事怎么办?

您似乎想要 CRTP 而不是公共基础 class 以避免重复代码:

template <typename > struct Bar;
template <template <typename, typename> class C, typename T1, typename T2>
struct Bar<C<T1, T2>>
{
     Bar(const Bar&) {/*..*/}

     template <typename U1, U2>
     Bar(const Bar<C<U1, U2>>&) {/*..*/}

     template <typename U1, U2>
     Bar(const C<U1, U2>&) {/*..*/}
};
// Maybe you just need template <template <typename, typename> class C> struct Bar{};
// instead, as T1, T2 seems not used

template<typename T, typename U>
struct foo : public bar<foo>
{ 
    using bar<foo>::bar;
};

template<typename T, typename U>
struct not_foo : public bar<not_foo>
{ 
  using bar<not_foo>::bar;
};