1

我以std::tuple.

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

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

4

1 回答 1

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());
}

变成

myFunction():
    movq    %rdi, %rax
    movl    $3, (%rdi)
    movq    $2, 8(%rdi)
    movl    $1, 16(%rdi)
    ret
f():
    movl    $1, %eax
    ret
于 2015-02-20T01:35:01.563 回答