以下代码编译:
struct Ret {};
struct A
{
virtual const Ret& fun() = 0;
};
struct B : public A
{
Ret& fun() override
{
static Ret ret;
return ret;
}
};
int main()
{
B b;
}
Run Code Online (Sandbox Code Playgroud)
如何在编译时禁止重写方法返回引用与返回类型的不同 const 说明符?
提前致谢。
我想实现一个通用函数,它将引用对象和指向其成员函数的指针并调用它。但是,当我的类同时具有 const 和非常量方法时,我无法这样做,因为我需要提供两个重载:
template<typename Ret, typename Class, typename ...Us>
Ret callMethod(Class &object, Ret (Class::*method)(Us...))
{
return (object.*method)(Us{}...);
}
template<typename Ret, typename Class, typename ...Us>
Ret callMethod(Class &object, Ret (Class::*method)(Us...) const)
{
return (object.*method)(Us{}...);
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以只编写 1 个模板函数来接受 const 和非常量方法指针,这样我就不必两次编写代码?我正在使用 C++14。
对于更广泛的图片,我想要最终实现的是传递第三个参数,一个从中提取方法参数的数据缓冲区 - 因此模板函数尽可能通用地处理它。
我希望能够形成一个只知道类本身和方法名称的成员指针类型。不幸的是,我无法在我的类中使用 const 和非常量方法变体。
示例代码片段:
struct A
{
void method() {}
void method() const {}
};
int main()
{
decltype(&A::method) _;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下方法,但也没有取得多大成功:
decltype(&(std::declval<const A>().method)) _;
Run Code Online (Sandbox Code Playgroud)
decltype由于歧义,这两种方法都失败了,因为无法解决这个问题:
'decltype cannot resolve address of overloaded function'
我怎样才能以其他方式实现这一目标?
不太确定我是否理解手册(或 vue.js)中的“非 prop 属性”: https: //v2.vuejs.org/v2/guide/components-props.html
假设我有 ChildComponent.vue 文件:
<template>
<input type="text" class="input" :value="childValue" v-on="listeners">
</template>
<script>
export default {
props: {
childValue: {
type: String,
default: 'blah',
}
},
computed: {
listeners() {
return {
// Pass all component listeners directly to input
...this.$listeners,
// Override input listener to work with v-model
input: event => this.$emit('input', event.target.value)
}
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
然后我将它添加到 ParentComponent 中,如下所示:
<template>
<ChildComponent v-model="parentValue" placeholder="default" @keydown.enter="parentMethod"/>
</template>
<script>
export default {
data () {
return …Run Code Online (Sandbox Code Playgroud)