不可调用项的示例赋值运算符
example assignment operator for non-invokables?
我有这个赋值运算符用于可调用的f
s。
template <typename F>
auto operator=(F&& f) -> decltype(f(), *this);
我还需要一个用于不可调用f
,因此在分配这些时不会有歧义。
template<class F>
auto assign_impl(F&& f, int) -> decltype((void) f(), *this) {
// f() is valid ...
}
template<class F>
auto assign_impl(F&& f, long) -> decltype(*this) {
// everything else
}
template<class F>
auto operator=(F&& f) -> decltype(*this) {
return assign_impl(std::forward<F>(f), 0);
}
我已经将 T.C. 的答案转换为类型特征,现在正在使用它。
namespace
{
template <typename F, typename ...A>
auto f(int) -> decltype(
(void)::std::declval<F>()(::std::declval<A>()...),
::std::true_type{}
);
template <typename F, typename ...A>
auto f(long) -> ::std::false_type;
}
template <typename F, typename ...A>
struct is_invokable : decltype(f<F, A...>(0))
{
};
相关文章:
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 为什么初始化时没有调用重载赋值运算符?
- 为什么我需要三个嵌套的大括号来调用赋值运算符,将const引用到二维数组
- 使用赋值运算符将宏转换为函数调用
- 从赋值运算符调用构造函数
- 赋值运算符重载在通过指针访问时不调用
- 为什么为单个赋值操作调用复制构造函数和重载赋值运算符
- 在其赋值运算符方法中调用对象的析构函数
- 为什么在没有赋值运算符的情况下调用转换构造函数
- 未调用移动赋值运算符
- 初始化对象后,用隐式转换而不是赋值运算符调用构造函数有什么意义
- C++重载赋值运算符在不相关的类中被调用
- 不可调用项的示例赋值运算符
- 为什么在这种情况下调用复制构造函数和赋值运算符
- 为什么deque::erase()调用赋值运算符
- C++移动语义:为什么调用复制赋值运算符=(&)而不是移动赋值运算符=(&&)?
- 为什么在这里调用复制构造函数而不是普通的构造函数和重载赋值运算符
- C++ 不调用赋值运算符
- 我可以从子类以外的另一个类调用抽象基类的公共赋值运算符吗?
- 如果类类型是按值调用的函数的参数,则会调用该类的重载赋值运算符吗?