这是我要执行的操作的最低版本:

template<typename D>
struct Base {

        void common() {
                // ... do something ...
                static_cast<D *>(this)->impl();
                // ... do something ...
        }

        void common_with_arg(typename D::Arg arg) {
                // ... do something ...
                static_cast<D *>(this)->impl_with_arg(arg);
                // ... do something more ...
        }
};

struct Derived : Base<Derived> {
        void impl() { }

        using Arg = int;
        void impl_with_arg(Arg arg) { }

};
Base::common()Derived::impl()工作正常(如预期)。Base::common_with_arg()Derived::impl_with_arg()却没有。

以gcc为例,出现以下错误:
1.cc: In instantiation of ‘struct Base<Derived>’:
1.cc:18:18:   required from here
1.cc:11:7: error: invalid use of incomplete type ‘struct Derived’
  void common_with_arg(typename D::Arg arg) {
       ^~~~~~~~~~~~~~~
1.cc:18:8: note: forward declaration of ‘struct Derived’
 struct Derived : Base<Derived> {

凭直觉(不了解有关模板实例化的所有详细信息),这似乎是一个明智的错误。还有另一种方法可以实现相同的功能吗?

最佳答案

void common_with_arg(typename D::Arg arg)
//                            ^^^^^^

您无法在此处访问D::Arg,因为需要Derived的定义。但是定义永远不可用,因为Base模板正在此处实例化...
struct Derived : Base<Derived> {
//               ^^^^^^^^^^^^^

... Derived尚未完全定义。

一种可能的解决方法是使common_with_arg为功能模板:
template <typename T>
void common_with_arg(T&& arg) {
        // ... do something ...
        static_cast<D *>(this)->impl_with_arg(std::forward<T>(arg));
        // ... do something more ...
}

example on wandbox

如果您确实需要Arg类型别名,请阅读以下问题:"C++ static polymorphism (CRTP) and using typedefs from derived classes"

关于c++ - CRTP:具有基于派生参数的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41551030/

10-11 17:52
查看更多