如何在未评估的上下文中将成员函数的结果类型应用于类成员
How can I get the result type of a member function applied to a class member in an unevaluated context
我什至不确定这个问题的标题是否正确。我想做的事对我来说是相当粗糙的,所以我什至不知道如何以简洁的方式描述它。抱歉。
我的值容器包裹在某种"安全"价值类中。我需要一个将指针指向该容器的函数,引用其成员之一,以下发生了:
如果指针传递是有效的,则该函数将返回包装值内部的值。
如果指针传递为 nullptr
,则该函数返回默认构造的值。
anyhoo,这里有一些代码。
template<typename T>
class Wrapped {
T t;
public:
T& operator*() {
return t;
}
};
class Container {
public:
Wrapped<int> i;
Wrapped<string> s;
};
// Compiler error with R.
// I'd like R to be the return type of invoking operator* on the member that's represented by M.
// I've tried about 50 different versions of declarations to declare the type of R. This one feels like it most closely represents what I'm trying to achieve.
// R should be the T in Wrapped<T>.
template <typename T, typename M, typename R = decltype(declval<M>().operator*())>
R value_or_default(T* object, M member, R default_value = R{})
{
object ? *((*object).*member) : default_value;
}
Container c;
auto actual_int = value_or_default(&c, &Container::i); // Returns *(c.i).
auto default_string = value_or_default(nullptr, &Container::s); // Returns string{}.
怎么样:
template<typename T>
class Wrapped {
T t{};
public:
T& operator*() {
return t;
}
};
class Container {
public:
Wrapped<int> i;
Wrapped<string> s;
};
template <typename T, typename R, typename C>
R value_or_default(T* object, Wrapped<R> C::* member)
{
return *((*object).*member);
}
template <typename R, typename C>
R value_or_default(nullptr_t, Wrapped<R> C::*, R default_value = R{})
{
return default_value;
}
int main() {
Container c;
auto actual_int = value_or_default(&c, &Container::i); // Returns *(c.i).
auto default_string = value_or_default(nullptr, &Container::s); // Returns string{}.
std::cout << actual_int << std::endl;
std::cout << default_string << std::endl;
return 0;
}
我想你可以写
template <typename T, typename M,
typename R = typename std::remove_reference<
decltype(*(std::declval<M>()))>::type>
R value_or_default (T * o, M T::*m)
{ return o ? *(o->*m) : R{}; }
但是您必须调用nullptr
案例,以说明对象的类型(T
),因为编译器无法从nullptr
推导。
so
Container c;
auto actual_int = value_or_default(&c, &Container::i); // Returns *(c.i).
auto default_string = value_or_default<Container>(nullptr, &Container::s);
// ....................................^^^^^^^^^
static_assert(std::is_same<int, decltype(actual_int)>::value, "!");
static_assert(std::is_same<std::string,
decltype(default_string)>::value, "!");
相关文章:
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 当在另一个范围中,将SelfAdjointeigensolver保存为成员的结果被重新引入
- 如何修改 std::generate 的绑定成员函数的结果
- 是否可以将类成员变量设置为类函数的结果?
- 多态性:成员访问和吸气器给出不同的结果
- 为什么 G++ 不警告 const 成员的未使用结果?
- 只有当声明一个对象时,内存空间才会分配给类的数据成员,那么为什么 Sizeof(class_name) 给我结果
- 向量的最后一个成员在 Rcpp 中没有给出正确的结果
- 如何在未评估的上下文中将成员函数的结果类型应用于类成员
- std::具有自定义比较函数结果的排序函数错误:必须调用对非静态成员函数的引用
- 将cv::Mat A的每个成员与cv::Mat B合并,以创建一个cv::Mat C,结果为[A(x,y),B(x,y)
- 返回成员变量时,为什么函数内外得到的结果不同
- 将重载+成员函数的结果发送到重载<<导致编译错误
- 使用函数调用结果在C++03中初始化多个成员
- C++成员函数结果缓存实现
- 将 std::forward_as_tuple() 结果传递给可能从该对象的 rvalue-reference 成员移动的多个函数?
- 重载成员函数访问元组和递归累加结果失败的原因
- 如何用函数的结果初始化静态成员数组
- 是否有可能将一个动作附加到boost::spirit::规则解析器,该解析器将解析结果分配给(尚未)未知实例的成员
- 如何仅使用元素成员函数和STL在容器中积累来自元素成员函数的结果