我需要在模板化类中实现 2 个函数,其中两个函数都做类似的事情,但并非一切都相同。我提出的解决方案是if constexpr在单个模板函数上使用,然后为每个函数设置一个别名:
template <typename T>
class MyClass
{
private:
template <bool test>
void TestFunc()
{
if constexpr(test)
{
// Do something
}
else
{
// Do other stuff
}
}
public:
?????? TestTrue = TestFunc<true>;
?????? TestFalse = TestFunc<false>;
}
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚什么应该去那里的问号是,到目前为止using,auto并const auto没有工作。我希望用户能够直接从类的对象中直接调用TestTrue()和调用TestFalse()。
你可以这样做:
void TestTrue() { TestFunc<true>(); }
void TestFalse() { TestFunc<false>(); }
Run Code Online (Sandbox Code Playgroud)
我不认为有更好的方法。
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |