# source\_location

***

**1. Getting the Source Location of a Function**

```cpp
#include <iostream>
#include <string>

int main() {
  auto source_location = __builtin_return_address(0);
  auto function_name = source_location->function_name;
  auto filename = source_location->file_name;
  auto line_number = source_location->line_number;

  std::cout << "Function: " << function_name << std::endl;
  std::cout << "File: " << filename << std::endl;
  std::cout << "Line: " << line_number << std::endl;

  return 0;
}
```

**2. Logging Source Location with GOOGLE\_LOG(Google Logging Library)**

```cpp
#include <glog/logging.h>

int main() {
  LOG(INFO) << "Source location: " << __FILE__ << ":" << __LINE__;
  return 0;
}
```

**3. Error Handling with Source Location**

```cpp
#include <iostream>

int divide(int a, int b) {
  if (b == 0) {
    throw std::runtime_error("Division by zero at " __FILE__ ":" __LINE__);
  }
  return a / b;
}

int main() {
  try {
    divide(10, 0);
  } catch (const std::exception& e) {
    std::cout << "Error: " << e.what() << std::endl;
  }
  return 0;
}
```

**4. Customizing Exception Behavior with Source Location**

```cpp
#include <iostream>
#include <sstream>

class MyException : public std::exception {
public:
  MyException(const char* file, int line) {
    std::stringstream ss;
    ss << "Error at " << file << ":" << line;
    message = ss.str();
  }

  const char* what() const noexcept override {
    return message.c_str();
  }

private:
  std::string message;
};

int main() {
  try {
    throw MyException(__FILE__, __LINE__);
  } catch (const MyException& e) {
    std::cout << "Error: " << e.what() << std::endl;
  }
  return 0;
}
```

**5. Debugging and Profiling with Source Location**

```cpp
#include <iostream>

int factorial(int n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}

int main() {
  int number = 5;
  int result = factorial(number);

  std::cout << "Factorial of " << number << " is " << result << std::endl;
#ifdef DEBUG
  std::cout << "Source location: " __FILE__ ":" __LINE__ << std::endl;
#endif
  return 0;
}
```

**6. Source Location in Unit Tests**

```cpp
#include <gtest/gtest.h>
#include <iostream>

TEST(MainTest, SourceLocation) {
  std::cout << "Source location: " << __FILE__ << ":" << __LINE__ << std::endl;
  ASSERT_EQ(1, 1);
}
```

**7. Logging with Timestamp and Source Location**

```cpp
#include <iostream>
#include <ctime>
#include <string>

int main() {
  std::time_t timestamp = std::time(nullptr);
  std::string timestamp_str = std::ctime(&timestamp);
  std::string file_name = __FILE__;
  int line_number = __LINE__;
  
  std::cout << timestamp_str << " " << file_name << ":" << line_number << " " << "Custom message" << std::endl;
  return 0;
}
```

**8. Source Location in Debug Logs**

```cpp
#include <iostream>

#ifdef DEBUG
#define LOG(msg) std::cout << "DEBUG: " << __FILE__ ":" << __LINE__ << " " << msg << std::endl
#else
#define LOG(msg)
#endif

int main() {
  LOG("Source location is available only in DEBUG mode");
  return 0;
}
```

**9. Exception Handling with Source Location and Backtrace**

```cpp
#include <exception>
#include <iostream>

void foo() {
  throw std::runtime_error("Exception in foo() at " __FILE__ ":" __LINE__);
}

void bar() {
  foo();
}

int main() {
  try {
    bar();
  } catch (const std::exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
  }
  return 0;
}
```

**10. Source Location in Template Metaprogramming**

```cpp
#include <iostream>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

int main() {
  std::cout << "Source location of main(): " << SourceLocation<int>::file << ":" << SourceLocation<int>::line << std::endl;
  return 0;
}
```

**11. Source Location in Macros**

```cpp
#define SOURCE_LOCATION __FILE__ ":" __LINE__

#include <iostream>

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION << std::endl;
  return 0;
}
```

**12. Source Location in Conditional Compilation**

```cpp
#include <iostream>

#ifdef DEBUG
  std::cout << "Source location: " __FILE__ ":" __LINE__ << std::endl;
#endif

int main() {
  return 0;
}
```

**13. Source Location in C++20 Concepts**

```cpp
#include <iostream>
#include <concepts>

template <typename T>
concept HasSourceLocation = requires(T t) {
  { t.file_name() } -> std::same_as<const char*>;
  { t.line_number() } -> std::same_as<int>;
};

int main() {
  std::cout << std::boolalpha << HasSourceLocation<std::source_location>() << std::endl;
  return 0;
}
```

**14. Source Location in Lambdas**

```cpp
#include <iostream>

int main() {
  auto lambda = [](auto x) -> std::pair<const char*, int> {
    return { __FILE__, __LINE__ };
  };

  auto source_location = lambda(42);
  std::cout << "Source location: " << source_location.first << ":" << source_location.second << std::endl;
  return 0;
}
```

**15. Source Location in Exception Handling**

```cpp
#include <iostream>

struct MyException : public std::exception {
  MyException(const std::source_location& location) : location_(location) {}

  const std::source_location& location() const { return location_; }

private:
  std::source_location location_;
};

int main() {
  try {
    throw MyException(std::source_location{ __FILE__, __LINE__ });
  } catch (const MyException& e) {
    std::cout << "Source location: " << e.location().file_name() << ":" << e.location().line_number() << std::endl;
  }
  return 0;
}
```

**16. Source Location in Templates with Variable Templates**

```cpp
#include <iostream>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename... Ts>
struct SourceLocation<std::tuple<Ts...>> {
  static constexpr const char* files[] = { SourceLocation<Ts>::file... };
  static constexpr int lines[] = { SourceLocation<Ts>::line... };
};

int main() {
  std::tuple<int, double, std::string> tuple;
  std::cout << "Source location: " << SourceLocation<decltype(tuple)>::files[1] << ":" << SourceLocation<decltype(tuple)>::lines[1] << std::endl;
  return 0;
}
```

**17. Source Location in Function Pointers**

```cpp
#include <iostream>

int add(int a, int b) {
  return a + b;
}

int main() {
  auto function_ptr = &add;
  std::cout << "Source location: " << function_ptr->source_location().file_name() << ":" << function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**18. Source Location in Lambda Expressions**

```cpp
#include <iostream>

int main() {
  auto lambda = [](int x, int y) {
    return x + y;
  };

  std::cout << "Source location: " << lambda.target<int(int, int)>().source_location().file_name() << ":" << lambda.target<int(int, int)>().source_location().line_number() << std::endl;
  return 0;
}
```

**19. Source Location in Variadic Templates**

```cpp
#include <iostream>

template <typename... Ts>
struct SourceLocation {
  static constexpr const char* files[] = { __FILE__... };
  static constexpr int lines[] = { __LINE__... };
};

int main() {
  SourceLocation<int, double, std::string> source_location;
  std::cout << "Source location: " << source_location.files[1] << ":" << source_location.lines[1] << std::endl;
  return 0;
}
```

**20. Source Location in Macros with Variable Arguments**

```cpp
#include <iostream>

#define SOURCE_LOCATION_VA(...) __FILE__ ":" __LINE__

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION_VA(42, "xyz") << std::endl;
  return 0;
}
```

**21. Source Location in Exception Handling with Stack Unwinding**

```cpp
#include <exception>
#include <iostream>

void foo() {
  throw std::runtime_error("Exception in foo() at " __FILE__ ":" __LINE__);
}

void bar() {
  foo();
}

int main() {
  try {
    bar();
  } catch (const std::exception& e) {
    std::cerr << "Exception: " << e.what() << " at " << e.source_location().file_name() << ":" << e.source_location().line_number() << std::endl;
  }
  return 0;
}
```

**22. Source Location in Template Metaprogramming with Fold Expressions**

```cpp
#include <iostream>

template <typename... Ts>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename... Ts>
struct FoldSourceLocation {
  static constexpr const char* file = (__VA_ARGS__::file, ...);
  static constexpr int line = (__VA_ARGS__::line, ...);
};

int main() {
  std::cout << "Source location: " << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::file << ":" << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::line << std::endl;
  return 0;
}
```

**23. Source Location in Library Functions**

```cpp
#include <iostream>
#include <vector>

int main() {
  std::vector<int> vec = { 1, 2, 3, 4, 5 };

  auto source_location = vec.begin()->source_location();
  std::cout << "Source location: " << source_location.file_name() << ":" << source_location.line_number() << std::endl;
  return 0;
}
```

**24. Source Location in Function Pointers with Anonymous Functions**

```cpp
#include <iostream>

int add(int a, int b) {
  return a + b;
}

int main() {
  int (*function_ptr)(int, int) = &add;

  std::cout << "Source location: " << function_ptr->source_location().file_name() << ":" << function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**25. Source Location in Generic Programming with Type Traits**

```cpp
#include <iostream>
#include <type_traits>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename T>
struct IsPOD {
  static constexpr bool value = std::is_pod<T>::value;
};

int main() {
  std::cout << "Source location: " << SourceLocation<std::is_pod<int>>::file << ":" << SourceLocation<std::is_pod<int>>::line << std::endl;
  return 0;
}
```

**26. Source Location in User-Defined Literals**

```cpp
#include <iostream>

constexpr std::source_location operator"" _source(const char* str, size_t) {
  return { __FILE__, __LINE__ };
}

int main() {
  auto source_location = "my_source"_source;
  std::cout << "Source location: " << source_location.file_name() << ":" << source_location.line_number() << std::endl;
  return 0;
}
```

**27. Source Location in Compile-Time Function Evaluation**

```cpp
#include <iostream>

template <typename T>
constexpr T eval(T x) {
  return x;
}

int main() {
  auto source_location = eval(__FILE__":__LINE__"_source);
  std::cout << "Source location: " << source_location.file_name() << ":" << source_location.line_number() << std::endl;
  return 0;
}
```

**28. Source Location in Template Metaprogramming with SFINAE**

```cpp
#include <iostream>
#include <type_traits>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename T>
constexpr bool has_source_location() {
  return std::is_same<decltype(T::file), const char*>::value && std::is_same<decltype(T::line), int>::value;
}

int main() {
  std::cout << "Has source location: " << has_source_location<SourceLocation<int>>() << std::endl;
  return 0;
}
```

**29. Source Location in Variadic Macros**

```cpp
#include <iostream>

#define SOURCE_LOCATION(...) __FILE__ ":" __LINE__

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION(42, "xyz") << std::endl;
  return 0;
}
```

**30. Source Location in Exception Handling with Nested Exceptions**

```cpp
#include <exception>
#include <iostream>

void foo() {
  try {
    throw std::runtime_error("Exception in foo() at " __FILE__ ":" __LINE__);
  } catch (const std::exception& e) {
    std::cerr << "Nested exception in foo(): " << e.what() << std::endl;
    throw;
  }
}

void bar() {
  foo();
}

int main() {
  try {
    bar();
  } catch (const std::exception& e) {
    std::cerr << "Exception in main(): " << e.what() << " at " << e.source_location().file_name() << ":" << e.source_location().line_number() << std::endl;
  }
  return 0;
}
```

**31. Source Location in Template Metaprogramming with Recursive Functions**

```cpp
#include <iostream>

template <int N>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <int N>
struct RecursiveSourceLocation {
  static constexpr const char* file = SourceLocation<N>::file;
  static constexpr int line = SourceLocation<N>::line;
};

int main() {
  std::cout << "Source location: " << RecursiveSourceLocation<42>::file << ":" << RecursiveSourceLocation<42>::line << std::endl;
  return 0;
}
```

**32. Source Location in Lambda Expressions with Captures**

```cpp
#include <iostream>

int main() {
  int x = 42;

  auto lambda = [&x](int y) {
    return x + y;
  };

  std::cout << "Source location: " << lambda.target<int(int)>().source_location().file_name() << ":" << lambda.target<int(int)>().source_location().line_number() << std::endl;
  return 0;
}
```

**33. Source Location in Macros with Stringification**

```cpp
#include <iostream>

#define SOURCE_LOCATION_STR(__x) #__x

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION_STR(__FILE__) << ":" << SOURCE_LOCATION_STR(__LINE__) << std::endl;
  return 0;
}
```

**34. Source Location in Function Pointers with Static Functions**

```cpp
#include <iostream>

struct MyStruct {
  static int add(int x, int y) {
    return x + y;
  }
};

int main() {
  auto function_ptr = &MyStruct::add;

  std::cout << "Source location: " << function_ptr->source_location().file_name() << ":" << function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**35. Source Location in Template Metaprogramming with Class Templates**

```cpp
#include <iostream>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename T>
struct ClassSourceLocation {
  static constexpr const char* file = SourceLocation<T>::file;
  static constexpr int line = SourceLocation<T>::line;
};

int main() {
  std::cout << "Source location: " << ClassSourceLocation<int>::file << ":" << ClassSourceLocation<int>::line << std::endl;
  return 0;
}
```

**36. Source Location in Exception Handling with Custom Exception Classes**

```cpp
#include <exception>
#include <iostream>

struct MyException : public std::exception {
  MyException(const std::source_location& location) : location_(location) {}

  const std::source_location& location() const { return location_; }

private:
  std::source_location location_;
};

int main() {
  try {
    throw MyException(std::source_location{ __FILE__, __LINE__ });
  } catch (const MyException& e) {
    std::cerr << "Exception: " << e.what() << " at " << e.location().file_name() << ":" << e.location().line_number() << std::endl;
  }
  return 0;
}
```

**37. Source Location in Function Pointers with Member Functions**

```cpp
#include <iostream>

struct MyStruct {
  int add(int x, int y) {
    return x + y;
  }
};

int main() {
  MyStruct my_struct;
  auto function_ptr = &MyStruct::add;

  std::cout << "Source location: " << function_ptr->source_location().file_name() << ":" << function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**38. Source Location in Template Metaprogramming with Fold Expressions and Strings**

```cpp
#include <iostream>

template <typename... Ts>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename... Ts>
struct FoldSourceLocation {
  static constexpr const char* file = (__VA_ARGS__::file, ...);
  static constexpr int line = (__VA_ARGS__::line, ...);
};

int main() {
  std::cout << "Source location: " << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::file << ":" << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::line << std::endl;
  return 0;
}
```

**39. Source Location in Macros with Variable Arguments and Stringification**

```cpp
#include <iostream>

#define SOURCE_LOCATION_VA_STR(...) #__VA_ARGS__

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION_VA_STR(42, "xyz") << std::endl;
  return 0;
}
```

**40. Source Location in Exception Handling with Stack Unwinding and Custom Exception Classes**

```cpp
#include <exception>
#include <iostream>

struct MyException : public std::exception {
  MyException(const std::source_location& location) : location_(location) {}

  const std::source_location& location() const { return location_; }

private:
  std::source_location location_;
};

void foo() {
  throw MyException(std::source_location{ __FILE__, __LINE__ });
}

void bar() {
  foo();
}

int main() {
  try {
    bar();
  } catch (const MyException& e) {
    std::cerr << "Exception: " << e.what() << " at " << e.location().file_name() << ":" << e.location().line_number() << std::endl;
  }
  return 0;
}
```

**41. Source Location in Function Pointers with Nested Functions**

```cpp
#include <iostream>

int add(int x, int y) {
  return x + y;
}

int main() {
  auto function_ptr = &add;

  int (*nested_function_ptr)(int, int) = [](int x, int y) {
    return function_ptr(x, y);
  };

  std::cout << "Source location: " << nested_function_ptr->source_location().file_name() << ":" << nested_function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**42. Source Location in Lambdas with Captures and Default Arguments**

```cpp
#include <iostream>

int main() {
  int x = 42;

  auto lambda = [x = 42](int y) {
    return x + y;
  };

  std::cout << "Source location: " << lambda.target<int(int)>().source_location().file_name() << ":" << lambda.target<int(int)>().source_location().line_number() << std::endl;
  return 0;
}
```

**43. Source Location in Macros with Stringification and Variable Arguments**

```cpp
#include <iostream>

#define SOURCE_LOCATION_VA_STR(...) #__VA_ARGS__

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION_VA_STR(42, "xyz") << std::endl;
  return 0;
}
```

**44. Source Location in Template Metaprogramming with Fold Expressions and Class Templates**

```cpp
#include <iostream>

template <typename... Ts>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename... Ts>
struct FoldSourceLocation {
  static constexpr const char* file = (__VA_ARGS__::file, ...);
  static constexpr int line = (__VA_ARGS__::line, ...);
};

int main() {
  std::cout << "Source location: " << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::file << ":" << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::line << std::endl;
  return 0;
}
```

**45. Source Location in Variadic Function Templates**

```cpp
#include <iostream>

template <typename... Ts>
void print_source_location(Ts... args) {
  (std::cout << ... << args.source_location().file_name() << ":" << args.source_location().line_number()) << std::endl;
}

int main() {
  print_source_location(1, 2.5, "hello");
  return 0;
}
```

**46. Source Location in Function Pointers with Member Functions and Nested Classes**

```cpp
#include <iostream>

struct OuterClass {
  struct InnerClass {
    int add(int x, int y) {
      return x + y;
    }
  };
};

int main() {
  OuterClass::InnerClass inner_class;
  auto function_ptr = &OuterClass::InnerClass::add;

  std::cout << "Source location: " << function_ptr->source_location().file_name() << ":" << function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**47. Source Location in Template Metaprogramming with Class Templates and Fold Expressions**

```cpp
#include <iostream>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename... Ts>
struct FoldSourceLocation {
  static constexpr const char* file = (__VA_ARGS__::file, ...);
  static constexpr int line = (__VA_ARGS__::line, ...);
};

int main() {
  std::cout << "Source location: " << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::file << ":" << FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>::line << std::endl;
  return 0;
}
```

**48. Source Location in Variadic Macros with Default Arguments**

```cpp
#include <iostream>

#define SOURCE_LOCATION(...) __FILE__ ":" __LINE__

int main() {
  std::cout << "Source location: " << SOURCE_LOCATION(42, "xyz") << std::endl;
  return 0;
}
```

**49. Source Location in Function Pointers with Lambda Expressions**

```cpp
#include <iostream>

int main() {
  auto lambda = [](int x, int y) {
    return x + y;
  };

  auto function_ptr = lambda;

  std::cout << "Source location: " << function_ptr->source_location().file_name() << ":" << function_ptr->source_location().line_number() << std::endl;
  return 0;
}
```

**50. Source Location in Template Metaprogramming with Fold Expressions and SFINAE**

```cpp
#include <iostream>
#include <type_traits>

template <typename T>
struct SourceLocation {
  static constexpr const char* file = __FILE__;
  static constexpr int line = __LINE__;
};

template <typename... Ts>
struct FoldSourceLocation {
  static constexpr const char* file = (__VA_ARGS__::file, ...);
  static constexpr int line = (__VA_ARGS__::line, ...);
};

template <typename T>
constexpr bool has_source_location() {
  return std::is_same<decltype(T::file), const char*>::value && std::is_same<decltype(T::line), int>::value;
}

int main() {
  std::cout << "Has source location: " << has_source_location<FoldSourceLocation<SourceLocation<int>, SourceLocation<double>, SourceLocation<std::string>>>() << std::endl;
  return 0;
}
```
