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

dav*_*ler 1 c++ c++11 g++4.8

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

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

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

Sti*_*sis 6

是的,它可以.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());
}
Run Code Online (Sandbox Code Playgroud)

myFunction():
    movq    %rdi, %rax
    movl    $3, (%rdi)
    movq    $2, 8(%rdi)
    movl    $1, 16(%rdi)
    ret
f():
    movl    $1, %eax
    ret
Run Code Online (Sandbox Code Playgroud)

  • @remyabel您是否看过gcc.godbolt.org提供的网址?它会自动缩短URL. (3认同)