如果模板参数是指针,则在getter上删除const限定符(const qualifier removed on getter if template argument is a pointer)

编程入门 行业动态 更新时间:2024-10-22 20:22:14
如果模板参数是指针,则在getter上删除const限定符(const qualifier removed on getter if template argument is a pointer)

我有以下代码:

template <typename T> struct Val { T val; const T& get() const { return val; } void set(const T& newVal) { val = newVal; } }; int& f(const Val<int>& val) { return val.get(); } int* g(const Val<int*>& val) { return val.get(); } int main() { Val<int> val1; f(val1); Val<int*> val2; g(val2); return 0; }

它使用以下消息在gcc中编译失败:

main.cpp: In function 'int& f(const Val<int>&)': main.cpp:78:20: error: invalid initialization of reference of type 'int&' from expression of type 'const int' return val.get(); ^ main.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]

这完全没问题,但为什么g()产生的错误不一样? 出于某种原因,为什么在const T&上删除了const限定符,当T是指针时?

我试图找到一些资源,但似乎很难找到。 我知道迈耶斯在他最新的书中写了一些东西,但我无法访问那本书。 有人可以指向我的资源或关键字,当T是指针时,我可以在模板类型推导的整个主题上找到更多信息吗?

I have the following code:

template <typename T> struct Val { T val; const T& get() const { return val; } void set(const T& newVal) { val = newVal; } }; int& f(const Val<int>& val) { return val.get(); } int* g(const Val<int*>& val) { return val.get(); } int main() { Val<int> val1; f(val1); Val<int*> val2; g(val2); return 0; }

It fails compilation in gcc with the following message:

main.cpp: In function 'int& f(const Val<int>&)': main.cpp:78:20: error: invalid initialization of reference of type 'int&' from expression of type 'const int' return val.get(); ^ main.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]

That's totally fine, but why isn't the same error produced for g()? Why, for some reason, is the const qualifier removed on const T&, when T is a pointer?

I tried to find some resources, but it seems hard to find. I know Meyers wrote something in his newest book, but I don't have access to that one. Can someone point me to resources or keywords where I could find more on that whole topic of template type deduction when T is a pointer?

最满意答案

出于某种原因,为什么在const T&上删除了const限定符,当T是指针时?

因为您按值返回指针,而不是通过引用。 从const指针初始化一个指针是完全没问题的(注意它是const的指针,而不是它们指向的东西。)你做的相当于:

int* const p0 = nullptr; // const pointer int* p1 = p0; // copy to non-const is OK

如果你要从const指针初始化poiter的非成本引用,你会得到与第一种情况类似的错误:

// error: binding of reference to type 'int *' // to a value of type 'int *const' drops qualifiers int*& p1 = p;

Why, for some reason, is the const qualifier removed on const T&, when T is a pointer?

Because you're returning the pointer by value, not by reference. It is perfectly fine to initialize a pointer from a const pointer (note it is the pointers that are const, not the things they point to.) You're doing the equivalent of this:

int* const p0 = nullptr; // const pointer int* p1 = p0; // copy to non-const is OK

If you were to initialize a non-cost reference to poiter from a const pointer, you'd get a similar error to the first case:

// error: binding of reference to type 'int *' // to a value of type 'int *const' drops qualifiers int*& p1 = p;

更多推荐

本文发布于:2023-08-05 06:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:则在   指针   模板   参数   getter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!