- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想为具有特定标记 (is_gpio_class) 的类型创建一个装饰器,该标记针对右值(必须复制)和左值(存储引用)以不同方式实现。为了避免提及类型,我使用了一个返回修饰类型的函数模板。
#include <type_traits>
template< typename T > struct always_false : std::false_type {};
// type with the tag to indicate that it is suitable
struct gpio_class {
typedef void is_gpio_class;
};
// fallback match gives an error
template< class P, class dummy = void >
struct invert_x {
static_assert( always_false< P >::value, "no match" );
};
// this should match an lvalue
template< class P >
struct invert_x< P, typename P::is_gpio_class > : public gpio_class {
constexpr invert_x( P & pin ) {}
};
// 'factory' function that dispatches to the correct specialization
template< typename P >
invert_x< P > invert( P && pin ){ return invert_x< P >( pin ); }
int main(){
gpio_class pin4;
// this works
auto led0 = invert_x< gpio_class>( pin4 );
// but this does not match the specialization
auto led1 = invert( pin4 );
}
这在函数具有 & 参数时有效,但为了区分 rval 和 lval,我认为我必须将其作为 && 传递,然后以某种方式以不同方式匹配左值和右值,但如何匹配?
最佳答案
您只需在 invert
调用 invert_x
时从推导的 P
中移除引用性。使用 std::remove_reference_t
:
template< typename P >
invert_x< P > invert( P && pin )
{
return invert_x< std::remove_reference_t<P> >( pin );
}
编辑:要支持右值和左值重载,请像这样使用标签分派(dispatch):
template< typename P >
invert_x< P > invert( P && pin )
{
return invert_x< std::remove_reference_t<P> >( pin,
std::is_lvalue_reference<P&&>());
}
并为 invert_x
的构造函数提供这些重载:
constexpr invert_x( P & pin, std::true_type/*This is the lvalue overload*/ ) {}
constexpr invert_x( P & pin, std::false_type/*This is the rvalue overload*/ ) {}
关于C++:如何专门针对左值和右值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28514189/
我是 C++ 的新手,正在尝试编写如下接口(interface): template class Comparable { protected: Comparable(){};
什么是左值? 最佳答案 左值是可以分配给以下值的值: lvalue = rvalue; 它是“left value”或“lefthand value”的缩写,它基本上只是=符号左侧的值,即您为其分配值
这个问题已经有答案了: Why do C and C++ support memberwise assignment of arrays within structs, but not general
C++03 $14.1/6 - "A non-type non-reference template-parameter is not an lvalue." C++0x $14.2/6- "A no
这只是出于好奇我问... 我在任何地方看到的 99% 的代码,当使用“IF”时,将采用“If (RValue == LValue) ...”格式。例子: If (variableABC == "Hel
在 C++03 中,表达式可以是右值,也可以是左值。 在 C++11 中,表达式可以是: 右值 左值 x值 gl值 右值 两个类别变成了五个类别。 这些新的表达类别是什么? 这些新类别与现有的右值和左
我正在尝试将 div 向左移动指定数量的像素,我正在使用从此处获得的 slider : http://carpe.ambiprospect.com/slider/archive/v1.3/ 所以从示例
所以我有一个函数需要将 std::vector 作为参数。我想知道声明参数的最佳方式,这样底层数组就不会被深度复制,因为它可能相当大。 // which should I choose? void m
在 C++03 中,表达式要么是右值,要么是左值。 在 C++11 中,表达式可以是: 右值 左值 xvalue glvalue 右值 两类变成了五类。 这些新的表达类别是什么? 这些新类别与现有的右
我正在尝试实现类似 find 的方法来从容器中提取对值的引用,如果找不到该值或该值的类型不兼容,则返回一个默认值。 template const T& get (key_type k, const T
我有以下代码: #include using namespace std; void test(int& a) { cout << "lvalue." << endl; } void tes
我的搜索发现许多关于右值绑定(bind)到左值的帖子,但没有任何类似的帖子。对不起,如果它是重复的。 struct StrHolder { StrHolder(std::string&& s)
这是一个后续问题 C++0x rvalue references and temporaries 在上一个问题中,我问过这段代码应该如何工作: void f(const std::string &);
为什么这不起作用: ostream& operator' lvalue to 'std::basic_ostream&&'| error: initializing argument 1 of '
偶尔,我被问到一个技巧问题:x++ 和 ++x 哪个不能在 c 中留值?很多人告诉我++x不能,因为++x的汇编代码不返回寄存器。我对此表示怀疑。所以我做了一些实验。 C 代码: #include
所以这个问题与我为实际问题选择的解决方案有关,为了尽可能提供完整的图片,这个问题是这样的:基本上我有一个带有很多模板参数的函数,我正在尝试暴露给 Python。与用宏做一些噩梦般的事情来消除我需要的所
指针很简单。有一些内存保存着一个地址。要获得(有意义的)值,取消引用返回地址指向的内存中包含的值。 引用以某种方式做一些类似的事情:它们持有指向一个临时对象的“链接”。但是当我分配或使用引用时会发生什
下面是一些示例代码: #include class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() {
背景 以下代码块出现在 Scott Meyers 的名著 "Effective C++" 中第 3 项: class TextBlock { public: ... const cha
我是一名优秀的程序员,十分优秀!