尝试根据模板类型重载模板 class 中的虚函数

Trying to overload a virtual function in a template class based on template type

我今天有 额外 有趣的恶作剧。

我对 C++ 中的模板有些陌生。这里有一些 classes,因为它们目前在我的代码中:

class location2d
{
    int x, y;
}

class location3d
{
    int x, y, z;
}

template <typename T>
class myClass : public parentClass<float, T>
{
    private:

    virtual void myFunction (T position) const override final
    {
        // some math stuff (this part doesn't matter)
        something = position.x + position.y;
    }

    int something;
};

现在这是硬编码到 location2d。如果传入 location3d,我需要 myFunction() 具有不同的行为。例如:

virtual void myFunction (T position) const override final
{
    something = position.x + position.y + position.z;
}

我已经阅读了有关模板专业化的内容,但这变得很棘手,因为 myFunction() 正在覆盖基 class 中的虚函数。我的理解是我们不能专门化一个虚函数。无论如何我试过了。它讨厌它。

我的第二个想法是对模板进行类型检查,然后调用单独的助手:

virtual void myFunction (T position) const override final
{
    if (std::is_same<T, location3d) {myFunction3(position);}
    else {myFunction2(position);}
}

void myFunction2 (T position) const
{
    something = position.x + position.y;
}

void myFunction3 (T position) const
{
    something = position.x + position.y + position.z;
}

这里的问题是编译器抛出“location2d 不包含成员'z'”,这是绝对正确的。但是,除非 z 存在,否则不会调用 myFunction3()。

接下来我尝试专门进行转换以使 T 不再含糊不清:

virtual void myFunction (T position) const override final
{
    if (std::is_same<T, location3d>::value) {myFunction3((location3d)position);}
    else {myFunction2((location2d)position);}
}

"'type cast': 无法从 'T' 转换为 'location3'".

最终想法: 由于辅助函数不是虚拟的,也许我可以专门化这两个函数。

virtual void myFunction (T position) const override final
{
    if (std::is_same<T, location3d>::value) {mySecondFunction<location3d>(position);}
    else {mySecondFunction<location2d>(position);}
}

template<>
void mySecondFunction<location2d> (location2d position) const {}

template<>
void mySecondFunction<location3d> (location3d position) const {}

我不确定我是否做错了,但它引发了大量我不知道如何修复的语法错误。

归根结底,我想要的只是让 myFunction() 的行为根据 'z' 是否存在而改变,我没有偏好它是如何完成的。我觉得我一定错过了一些简单的东西。

您进行类型检查的想法是正确的,但您的方法需要做更多的工作来帮助编译器。

如果您使用的是 C++17 或更高版本,请使用 if constexpr with std::is_same_v,例如:

template <typename T>
class myClass : public parentClass<float, T>
{
private:
    virtual void myFunction (T position) const override final
    {
        if constexpr (std::is_same_v<T, location3d>) {
            something = position.x + position.y + position.z;
        }
        else {
            something = position.x + position.y;
        }
    }

    int something;
};

编译器将在 compile-time 处完全评估 if constexpr 并消除最终运行时代码中未使用的分支,从而为 myClass<T> 的每个实例化生成不同的代码,例如:

class myClass<location2d> : public parentClass<float, location2d>
{
private:
    virtual void myFunction (location2d position) const override final
    {
        something = position.x + position.y;
    }

    int something;
};

class myClass<location3d> : public parentClass<float, location3d>
{
private:
    virtual void myFunction (location3d position) const override final
    {
        something = position.x + position.y + position.z;
    }

    int something;
};

如果您不适合使用 C++17 或更高版本,那么您可以使用 reinterpret_cast,例如:

template <typename T>
class myClass : public parentClass<float, T>
{
private:
    virtual void myFunction (T position) const override final
    {
        if (std::is_same<T, location3d>::value) {
            // if T is NOT location3d then accessing position.z as-is
            // will fail to compile if T::z is missing, hence the cast.
            // Since this branch is executed only when T is location3d,
            // the cast in this branch is redundant but harmless. But
            // this branch is still compiled even when T is NOT loction3d...
            something = position.x + position.y + reinterpret_cast<location3d&>(position).z;
        }
        else {
            // no cast is needed here since location2d and location3d
            // both have x and y fields...
            something = position.x + position.y;
        }
    }

    int something;
};

如果没有强制转换,编译器将为 myClass<T> 的每个实例生成如下代码:

class myClass<location2d> : public parentClass<float, location2d>
{
private:
    virtual void myFunction (location2d position) const override final
    {
        if (false) {
            something = position.x + position.y + position.z; // ERROR! location2d::z does not exist...
        }
        else {
            something = position.x + position.y; // OK
        }
    }

    int something;
};

class myClass<location3d> : public parentClass<float, location3d>
{
private:
    virtual void myFunction (location3d position) const override final
    {
        if (true) {
            something = position.x + position.y + position.z; // OK
        }
        else {
            something = position.x + position.y; // OK
        }
    }

    int something;
};

position 传递给 non-template 成员方法时会发生同样的问题,例如:

template <typename T>
class myClass : public parentClass<float, T>
{
private:
    virtual void myFunction (T position) const override final
    {
        if (std::is_same<T, location3d) {
            // if T is NOT location3d, passing position as-is to myFunction3()
            // would fail to compile, hence the cast. Since this branch is
            // executed only when T is location3d, the cast in this branch
            // is redundant but harmless. But this branch is still compiled
            // even when T is NOT loction3d...
            myFunction3(reinterpret_cast<location3d&>(position));
        }
        else {
            // if T is NOT location2d, passing position as-is to myFunction2()
            // would fail to compile, hence the cast. Since this branch is
            // executed only when T is location2d, the cast in this branch
            // is redundant but harmless. But this branch is still compiled
            // even when T is NOT location2d...
            myFunction2(reinterpret_cast<location2d>(position));
        }
    }

    void myFunction2 (location2d position)
    {
        something = position.x + position.y;
    }

    void myFunction3 (location3d position)
    {
        something = position.x + position.y + position.z;
    }

    int something;
};

如果没有强制转换,编译器将为 myClass<T> 的每个实例化生成如下代码:

class myClass<location2d> : public parentClass<float, location2d>
{
private:
    virtual void myFunction (location2d position) const override final
    {
        if (false) {
            myFunction3(position); // ERROR! can't convert from location2d to location3d
        }
        else {
            myFunction2(position); // OK
        }
    }

    void myFunction2 (location2d position)
    {
        something = position.x + position.y;
    }

    void myFunction3 (location3d position)
    {
        something = position.x + position.y + position.z;
    }

    int something;
};

class myClass<location3d> : public parentClass<float, location3d>
{
private:
    virtual void myFunction (location3d position) const override final
    {
        if (true) {
            myFunction3(position); // OK
        }
        else {
            myFunction2(position); // ERROR! can't convert from location3d to location2d
        }
    }

    void myFunction2 (location2d position)
    {
        something = position.x + position.y;
    }

    void myFunction3 (location3d position)
    {
        something = position.x + position.y + position.z;
    }

    int something;
};

话虽这么说,另一种选择是使用模板特化,然后根本不需要时髦的转换,例如:

template<typename T>
int add_them_up(T) { return 0; }

template<>
int add_them_up<location2d>(location2d position)
{
    return position.x + position.y;
}

template<>
int add_them_up<location3d>(location3d position)
{
    return position.x + position.y + position.z;
}

template <typename T>
class myClass : public parentClass<float, T>
{
private:
    virtual void myFunction (T position) const override final
    {
        something = add_them_up<T>(position);
    }

    int something;
};

编译器将为 myClass<T> 的每个实例化生成如下代码:

int add_them_up<location2d>(location2d position)
{
    return position.x + position.y;
}

int add_them_up<location3d>(location3d position)
{
    return position.x + position.y + position.z;
}

class myClass<location2d> : public parentClass<float, location2d>
{
private:
    virtual void myFunction (location2d position) const override final
    {
        something = add_them_up<location2d>(position);
    }

    int something;
};

class myClass<location3d> : public parentClass<float, location3d>
{
private:
    virtual void myFunction (location3d position) const override final
    {
        something = add_them_up<location3d>(position);
    }

    int something;
};

编译器在调用点内联专门函数后,看起来非常熟悉1

1: <咳咳> C++17 if constexpr 输出 !

class myClass<location2d> : public parentClass<float, location2d>
{
private:
    virtual void myFunction (location2d position) const override final
    {
        something = position.x + position.y;
    }

    int something;
};

class myClass<location3d> : public parentClass<float, location3d>
{
private:
    virtual void myFunction (location3d position) const override final
    {
        something = position.x + position.y + position.z;
    }

    int something;
};