gpt4 book ai didi

c++ - 元组作为返回类型,是否优化了未访问的值?

转载 作者:行者123 更新时间:2023-11-30 00:49:16 24 4
gpt4 key购买 nike

我以 std::tuple 的形式从函数返回三个项目。

... myFunction()
{
...
return std::tuple< int, unsigned long long, unsigned int >{ errorCode, timeStamp, sizeOfBuffer };
}

由于必须使用 std::getstd::tie 访问返回值,编译器是否针对未使用的值进行了优化 (g++ 4.8)?

最佳答案

是的,它可以。 http://goo.gl/UB7DNc

#include "stdio.h"
#include <tuple>

std::tuple<int, unsigned long long, unsigned int> myFunction()
{
return std::tuple<int, unsigned long long, unsigned int>{ 1, 2, 3 };
}

int f()
{
return std::get<0>(myFunction());
}

成为

myFunction():
movq %rdi, %rax
movl $3, (%rdi)
movq $2, 8(%rdi)
movl $1, 16(%rdi)
ret
f():
movl $1, %eax
ret

关于c++ - 元组作为返回类型,是否优化了未访问的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com