You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++函数模板重载引发“返回局部临时对象引用”警告的问题咨询

C++函数模板重载引发“返回局部临时对象引用”警告的问题咨询

各位C大佬好!我最近在啃《C Templates: The Complete Guide 2nd Edition》这本书,跟着里面的示例写函数模板重载的代码,结果编译时触发了**“返回局部临时对象引用”**的警告,想过来请教下大家问题出在哪,该怎么解决?

先把我用到的代码贴出来:

#include <cstring>
#include <iostream>

template<typename T>
T const& max(T const &a, T const &b){
    std::cout << "max ref 2\n";
    return b<a ? a : b;
}

char const* max(char const *a, char const *b){
    std::cout << "max char 2\n";
    return (std::strcmp(b, a) < 

注:代码是从书中摘录的,可能复制时没截全,但核心的重载逻辑就是这样,麻烦各位帮忙看看啦!

备注:内容来源于stack exchange,提问作者heturing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 16:53:14