Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #126 Multiple return values

Write a function foo that returns a string and a boolean value.

#include <tuple>
std::tuple<std::string, bool> foo() {
  return std::make_tuple("someString", true);
}

c++ 11
#include <tuple>
auto foo()
{
    return std::make_tuple("Hello", true);
}

use automatic return type deduction to save having to repeat the type
#include <stdbool.h>
typedef struct {
    const char * const a;
    const bool b;
} RetStringBool;

RetStringBool foo() {
    return (RetStringBool) {.a = "Hello", .b = true};
}

New implementation...
< >
MLKo