【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()));
相关推荐
汉克老师40 分钟前
GESP2025年3月认证C++二级( 第三部分编程题(1)等差矩阵)
c++·算法·矩阵·gesp二级·gesp2级
joker D8881 小时前
【C++】深入理解 unordered 容器、布隆过滤器与分布式一致性哈希
c++·分布式·哈希算法
野曙2 小时前
快速选择算法:优化大数据中的 Top-K 问题
大数据·数据结构·c++·算法·第k小·第k大
休息一下接着来3 小时前
C++ 条件变量与线程通知机制:std::condition_variable
开发语言·c++·算法
爱学习的小邓同学3 小时前
C++ --- new与delete
c++
努力学习的小廉3 小时前
【C++】 —— 笔试刷题day_29
开发语言·c++·算法
六bring个六4 小时前
文件系统交互实现
开发语言·c++·qt·交互
小山菌4 小时前
mac中加载C++动态库文件
开发语言·c++·macos
疯狂学习GIS4 小时前
Windows配置VS Code详细流程
c++·学术工作效率
__BMGT()4 小时前
C++ QT图片查看器
前端·c++·qt