我需要在模板化类中实现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>;
}
我试图弄清楚什么应该去那里的问号是,到目前为止using
,auto
并const auto
没有工作。我希望用户能够调用TestTrue()
和TestFalse()
直接从类的对象直接。
您可以这样做:
void TestTrue() { TestFunc<true>(); }
void TestFalse() { TestFunc<false>(); }
我认为没有更好的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句