element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog modern C++: write my own function that returns true / false, and a value, part 2 (embedded friendly)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 4 Dec 2025 5:59 PM Date Created
  • Views 33 views
  • Likes 3 likes
  • Comments 2 comments
  • gcc
  • Modern C++
  • c++26
  • c++
Related
Recommended

modern C++: write my own function that returns true / false, and a value, part 2 (embedded friendly)

Jan Cumps
Jan Cumps
4 Dec 2025

what came before:

  •  modern C++: function returns true / false, and a value or error message 
  •  modern C++: write my own function that returns true / false, and a value or error message 

C++ has released a new feature: return value parameter binding. It's a somewhat obscure name (some say: just like C++ itself Slight smile). Here is the short version of what it gives us:

A function (or member), traditionally, could return 1 "return value". Now, this value can be several things, captured in a class. And you can run a method of that return value while calling the function.

Example: you can have a parser function, that returns

  • the parsed values (several things)
  • and it can return a boolean success / failure at the same time. (so you can use if (myfunc() ... )

I've implemented this in a GPS message parser I wrote.

Original user code: the function returns a bool, and the data is returned via the i/o parameter o)

std::string reply = "$GPGLL,5051.83778,N,00422.55809,S,185427.150,V,N*4F";
nmea::gll o;
if (nmea::gll::from_data(reply, o)) { // data exchanged via an in/out parameter
  // do stuff
}

With return value parameter binding: all outputs are returned: data and status. And the if makes a decision on that status.

std::string reply = "$GPGLL,5051.83778,N,00422.55809,S,185427.150,V,N*4F";
if (auto [result, success] = nmea::gll::from_data(reply)) {
  // do stuff. result is the same object as o in the original code.
}

Spectacular? Maybe yes:

getting results as return values is neater than getting results via pointers or reference i/o parameters. And we don't have to choose between returning a status or a data set. We can do both.

In this example, the parsed value is available as the bound parameter result. And we call the result's function operator bool() (the if clause does).

This means that we can call the function and

  • decide what to do based on success/failure: the if statement
  • receive the results too, in our result "variable" (it's actually a bound parameter)

If you're interested in how this is coded, and how it's handled, leave a comment. The code is published on github.

  • Sign in to reply
  • DAB
    DAB 2 hours ago

    Nice post Jan.

    I like the idea of getting multiple types of information directly from functions.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 5 hours ago

    I flagged this as embedded friendly, but there is a caveat for ARM controllers:

    It needs C++ 15. If you are using a Raspberry Pico 2, in RISC-V mode, it will work. GCC15 is released for that.

    But as of today, there is no published ARM GCC 15 toolchain. If the OpenArm initiative sticks to its schedule, a version 15 release is imminent...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube