【VS2022 编译UE5.1 错误 C4834 】

这里写自定义目录标题

错误

使用VS2022编译UE5.1源码,错误 C4834 放弃具有 [[nodiscard]] 属性的函数的返回值

复制代码
F:\UE\Engine\Plugins\Runtime\Steam\SteamVR\Source\SteamVRInputDevice\Private\SteamVRInputDeviceFunctionLibrary.cpp(513): error C4834: discarding return value of function with 'nodiscard' attribute

原因

This is a bug in the UE code on line 513.

It should probably be:

复制代码
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Unable to find Action [%s] for Action Set [%s]"), *ActionName.ToString(), *ActionSet.ToString()));

instead of:

复制代码
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, (TEXT("Unable to find Action [%s] for Action Set [%s]"), *ActionName.ToString(), *ActionSet.ToString()));

Actually the problem is in the substring:

复制代码
(TEXT("Unable to find Action [%s] for Action Set [%s]"), *ActionName.ToString(), *ActionSet.ToString())

This line is like:

复制代码
const TCHAR *value = (TEXT("some value"), name1.CalculateString(), name2.CalculateString());

or

复制代码
auto a = (op1(), op2(), op3());

the results of calling op1 and op2 will be ignored, but only op3() will be assigned to a.

So, if op1 or op2 has a "nodiscard" attribute, Visual Studio will generate an error/warning.

The previous version of Visual Studio did not have this behavior. New Visual Studio update - has.

The actual nodiscard attribute that can cause this error in this situation is UnrealString.h, line 324:

复制代码
/**
 * Get pointer to the string
 *
 * @Return Pointer to Array of TCHAR if Num, otherwise the empty string
 */
UE_NODISCARD FORCEINLINE const TCHAR* operator*() const UE_LIFETIMEBOUND
{
	return Data.Num() ? Data.GetData() : TEXT("");
}

To sum up, the solution would be to use this line instead of line 513:

复制代码
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Unable to find Action [%s] for Action Set [%s]"), *ActionName.ToString(), *ActionSet.ToString()));
相关推荐
Benny_Tang15 小时前
题解:P14841 [THUPC 2026 初赛] 哈姆星与古地球学术行为影响星球文明的考古学分析
c++·算法
迷途之人不知返15 小时前
C++初识(3)
c++
王老师青少年编程16 小时前
2024年9月GESP真题及题解(C++七级): 矩阵移动
c++·算法·题解·真题·gesp·七级·矩阵移动
txinyu的博客16 小时前
连接池问题
服务器·网络·c++
初願致夕霞16 小时前
实现具备C++11现代特性的STL——list篇(使用shared_ptr智能指针实现,解决了循环引用问题)
c++·list
雾岛听蓝16 小时前
AVL树实现
开发语言·c++
苏宸啊16 小时前
C++模版template(泛型编程)初阶
c++
郝学胜-神的一滴16 小时前
Qt自定义TabWidget:实现左侧标签与水平文本布局
开发语言·c++·qt·程序人生
晚风吹长发16 小时前
初步理解Linux中的进程间通信以及管道通信
linux·运维·服务器·c++·进程·通信
燃于AC之乐16 小时前
C/C++内存管理核心解析:分布、管理方式与定位new应用
开发语言·c++·内存管理