Tuesday Coding Tip 12 — Stack trace

●5 ●35 ●98
calendar_today • schedule1 min read
— Originally published at medium.com

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


C++23 brings a new feature that I always wanted since I’ve seen it in C# — stack trace.

When combined with exceptions, it can immensely help with the analysis of user error reports, when you cannot simply attach a debugger. The code snippet shows how to create an exception class that can automatically capture stack trace at the point where it was constructed.

#include <iostream>
#include <stacktrace>
#include <stdexcept>

class stacktrace_error : public std::runtime_error
{
public:
    stacktrace_error(std::stacktrace trace = std::stacktrace::current())
        : std::runtime_error(std::to_string(trace))
    {}
};

void bar() { throw stacktrace_error(); }
void foo() { bar(); }

int main()
{
    try
    {
        foo();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        /* OUTPUTS:
        0> C:\...\Main.cpp(19): Sandbox!bar+0x44
        1> C:\...\Main.cpp(25): Sandbox!foo+0x20
        2> C:\...\Main.cpp(31): Sandbox!main+0x32
        3> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(79): Sandbox!invoke_main+0x39
        4> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288): Sandbox!__scrt_common_main_seh+0x12E
        5> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(331): Sandbox!__scrt_common_main+0xE
        6> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp(17): Sandbox!mainCRTStartup+0xE
        7> KERNEL32!BaseThreadInitThunk+0x14
        8> ntdll!RtlUserThreadStart+0x21
        */
    }
}

The API even allows you to iterate over each stack entry, but honestly — who needs that?

Note: Linux versions of GCC and Clang have this stupid idea of requiring you to manually link stacktrace (akin to math library), because -std=c++23 is apparently not enough of a feature flag. So don't forget to use -lstdc++_libbacktrace argument for compilation.

Want more #programming & #cpp deep dives?

Join 6,110+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 12 of 32 in Tuesday Coding Tips

1 Comment

1 vote

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Let's play with C++ reflection

Jakub Neruda - Nov 12

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30

Tuesday Coding Tip 31 — Deducing this

Jakub Neruda - Nov 17
chevron_left
2.2k Points • 138 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
47Posts
40Comments
25Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Commenters (This Week)

4 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!