元组作为一种返回类型,是经过优化的未处理值

Tuple as a return type, are unaccessed values optimized out?

本文关键字:经过 优化 未处理 返回类型 一种 元组      更新时间:2023-10-16

我以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