Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::source_location

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
       
      Defined in header<source_location>
      struct source_location;
      (since C++20)

      Thestd::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 macros so thatpredefined macros like__LINE__ and__FILE__ are expanded in the context of the caller. Thestd::source_location class provides a better alternative.

      std::source_location meets theDefaultConstructible,CopyConstructible,CopyAssignable,Destructible andSwappable requirements.

      Additionally, the following conditions aretrue:

      It is intended thatstd::source_location has a small size and can be copied efficiently.

      It is unspecified whether the copy/move constructors and the copy/move assignment operators ofstd::source_location are trivial and/or constexpr.

      Contents

      [edit]Member functions

      Creation
      constructs a newsource_location with implementation-defined values
      (public member function)[edit]
      [static]
      constructs a newsource_location corresponding to the location of the call site
      (public static member function)[edit]
      Field access
      return the line number represented by this object
      (public member function)[edit]
      return the column number represented by this object
      (public member function)[edit]
      return the file name represented by this object
      (public member function)[edit]
      return the name of the function represented by this object, if any
      (public member function)[edit]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_source_location201907L(C++20)Source-code information capture (std::source_location)

      [edit]Example

      Run this code
      #include <iostream>#include <source_location>#include <string_view> void log(conststd::string_view message,const std::source_location location=               std::source_location::current()){std::clog<<"file: "<< location.file_name()<<'('<< location.line()<<':'<< location.column()<<") `"<< location.function_name()<<"`: "<< message<<'\n';} template<typename T>void fun(T x){    log(x);// line 20} int main(int,char*[]){    log("Hello world!");// line 25    fun("Hello C++20!");}

      Possible output:

      file: main.cpp(25:8) `int main(int, char**)`: Hello world!file: main.cpp(20:8) `void fun(T) [with T = const char*]`: Hello C++20!

      [edit]See also

      changes the source code's line number and, optionally, the current file name
      (preprocessing directive)[edit]
      representation of an evaluation in a stacktrace
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/source_location&oldid=177178"

      [8]ページ先頭

      ©2009-2025 Movatter.jp