惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - BackSword

unity ui canvas shader texcoord.zw is not used for ui particle 问题记录,unity shaderlab 模版写入问题 textmeshpro 放大缩小出现黑色边框问题,修改shader FXAA 在桌面平台更高效的原因,MSAA在手机端更高效 glados优惠码 C# Array.Fill 值类型优化。 git 设置github代理 unity physics bug win10 ctrl+space 快捷键冲突问题 msvc C++编译链接 切线空间 c++局部静态变量是线程安全的 c++函数参数和返回值 c++返回值不能是右值对象 状态同步 分享mkgmttime自实现功能。 关于socket通信中大小端转换问题 wpf clickonece 坑 [修复] 启动期间超频失败的错误信息
template return value error C2440: “初始化”: 无法从“const T”转换为“const Player *&”
BackSword · 2021-09-22 · via 博客园 - BackSword

模板返回值参数,和const T&, T const&问题。

1.我有如下模板,当类型为不带*参数的时,一切正常

template<typename T>
const T & Get(const T & aa)
{
    const T & b = aa;
    std::cout << b << std::endl;
    return b;
}
int main(int, char **)
{
    int a = 10;
    const int& b = Get<int>(a);    
}

2. 当使用带*指针类型的话,报错

error C2440: “初始化”: 无法从“const T”转换为“const Player *&”

[build]           with

[build]           [

[build]               T=Player *

[build]           ]

看代码21行,用Player* const& 接就可以正常编译通过,因为T是Player*, 所以引用类型是 Player* const& 不让改指向,const Player* & 可以改指向, 但是不能改值。

template<typename T>
const T & Get(const T & aa)
{
    const T & b = aa;
    std::cout << b << std::endl;
    return b;
}

class Player
{
public:
    void func()
    {
        Player* pp = new Player();
        std::cout << pp << std::endl;
        // Player* const& cc = Get<Player*>(pp); // 这个为正常的情况 
        const Player* & cc = Get<Player*>(pp);// 这个编译报错。
        std::cout << pp << std::endl;
    }
    int a = 10;
};

int main(int, char **)
{
    int a = 10;
    const int& b = Get<int>(a);
    Player cc;
    cc.func();  
}