std::experimental::source_location (3) - Linux Manuals

std::experimental::source_location: std::experimental::source_location

NAME

std::experimental::source_location - std::experimental::source_location

Synopsis


Defined in header <experimental/source_location>
struct source_location; (library fundamentals TS v2)


The source_location class represents certain information about the source code, such as file names, line numbers, and function names. Previously, functions that desire to obtain this information about the call site (for logging, testing, or debugging purposes) must use function-style macros so that predefined macros like __LINE__ and __FILE__ are expanded in the context of the caller. The source_location class provides a better alternative.

Member functions


 Creation


                      constructs a new source_location with implementation-defined values
constructor (public member function)


current constructs a new source_location
                      (public static member function)
[static]


 Other special member functions


destructor destructs a source_location
                      (public member function)
(implicitly declared)


operator= Implicitly declared copy/move assignment operators
                      (public member function)
(implicitly declared)


 Field access


                      return the line number represented by this object
line (public member function)
                      return the column number represented by this object
column (public member function)
                      return the file name represented by this object
file_name (public member function)
                      return the name of the function represented by this object, if any
function_name (public member function)

Example


// Run this code


  #include <iostream>
  #include <string_view>
  #include <experimental/source_location>


  void log(const std::string_view& message, const std::experimental::source_location& location = std::experimental::source_location::current())
  {
      std::cout << "info:"
                << location.file_name() << ":"
                << location.line() << " "
                << message << '\n';
  }


  int main()
  {
      log("Hello world!");
  }

Possible output:


  info:main.cpp:15 Hello world!