将成员声明与enable_if一起使用
Using member declaration with enable_if?
我需要条件使用成员声明。
template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };
template <typename T>
struct A : public B<is_default_constructible<T>::value> {
using B<is_default_constructible<T>::value>::foo();
void foo(int) {}
};
这显然不起作用,因为B<bool>::foo
没有定义在一半的情况下。我怎样才能做到这一点?拥有B<>::foo()
在foo(int)旁边的A<T>
作用域中是否可见?
这是我的解决方案。我相信这不会是最好的,但它完成了任务。
struct A {
void foo(int) {}
};
struct A
应该包含在这两种情况下都要定义的方法。
template <bool> struct B;
template <> struct B<false> : A {};
template <> struct B<true> : A {
using A::foo;
void foo() {}
};
在B<false>
的情况下,仅定义void foo(int)
。在B<true>
的情况下,定义了void foo(int)
和void foo()
。
template <typename T>
struct C : public B<is_default_constructible<T>::value> {};
现在我不必担心B<is_default_constructible<T>::value>::foo()
在某些情况下没有定义。
class D { D() = delete; };
int main()
{
C<int> c1;
c1.foo(1234);
c1.foo();
// both methods are defined for C<int>
C<D> c2;
c2.foo(1234);
// c2.foo(); // undefined method
return 0;
}
使用专业化
enable_if
不能用于此。你也需要专门化struct A
。
#include <type_traits>
template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };
template <typename T, bool default_constructible = std::is_default_constructible<T>::value>
struct A : public B<default_constructible> {
using B<default_constructible>::foo;
void foo(int) {}
};
template<typename T>
struct A<T, false> : public B<false> {
void foo(int) {}
};
避免foo(int)的重复代码
如果foo(int)
在这两种情况下都具有相同的功能,您可能需要从另一个基本结构中派生它:
#include <type_traits>
template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };
template<typename T>
struct C {
void foo(int) {}
};
template <typename T, bool default_constructible = std::is_default_constructible<T>::value>
struct A : public B<default_constructible>, public C<T> {
using B<default_constructible>::foo;
using C<T>::foo;
};
template<typename T>
struct A<T, false> : public B<false>, public C<T> {
using C<T>::foo;
};
消除那丑陋的嘘声
最后,要从struct A
的模板参数中删除bool,您可能需要将选择foo
重载的责任转发给基类。这还有一个优点,即不会为您可能想要添加的其他struct A
成员复制代码。
#include <type_traits>
template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };
template<typename T>
struct C {
void foo(int) {}
};
template <typename T, bool default_constructible = std::is_default_constructible<T>::value>
struct base_A : public B<default_constructible>, public C<T> {
using B<default_constructible>::foo;
using C<T>::foo;
};
template<typename T>
struct base_A<T, false> : public B<false>, public C<T> {
using C<T>::foo;
};
template <typename T>
struct A : public base_A<T> {
// Other members.
};
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- 我如何将 OR 与 if 语句一起使用字符串
- 如何在 C++17 中将 "else-if" 与初始值设定项一起使用?
- 将 if else 与开关一起使用
- typedef的替代品或将typedef与if-else一起使用的正确方法
- 如何将用户输入与if语句组合在一起
- 我可以在 IF 语句中将比较链接在一起吗?
- 将 if 语句与指针(BST 树)一起使用时出现分段错误