是否有标准的 Untokenize 模板化类型?
Is there a Standard Untokenize Templated Type?
是否有标准类型来取消标记类型?它可能会这样实现:
template<class T>
using untokenize = T;
这样我就可以使用重载运算符执行以下转换:
struct x {
int y;
operator int&() {
return y;
}
};
x a;
// int&(a); // doesn't work
// (int&)(a); // not the same thing
untokenize<int&>(a); // works great
或者是否有另一种更标准的方法来实现我的目标,即避免使用 C 风格的强制转换,转而使用函数风格的强制转换?
static_cast<T>
满足您的要求。
一般来说,使用正确的“命名演员表”会更加地道。这里,static_cast
是合适的:
static_cast<int&>(a);
也就是说,是一个标准模板,其工作方式与您的其他方法相同,std::type_identity
,但仅限于 C++20:
std::type_identity_t<int&>(a);
是否有标准类型来取消标记类型?它可能会这样实现:
template<class T>
using untokenize = T;
这样我就可以使用重载运算符执行以下转换:
struct x {
int y;
operator int&() {
return y;
}
};
x a;
// int&(a); // doesn't work
// (int&)(a); // not the same thing
untokenize<int&>(a); // works great
或者是否有另一种更标准的方法来实现我的目标,即避免使用 C 风格的强制转换,转而使用函数风格的强制转换?
static_cast<T>
满足您的要求。
一般来说,使用正确的“命名演员表”会更加地道。这里,static_cast
是合适的:
static_cast<int&>(a);
也就是说,是一个标准模板,其工作方式与您的其他方法相同,std::type_identity
,但仅限于 C++20:
std::type_identity_t<int&>(a);