Skip to content

Instantly share code, notes, and snippets.

@uemuraj
Created December 31, 2021 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uemuraj/213816a1180acc48ec7755b541079c5e to your computer and use it in GitHub Desktop.
Save uemuraj/213816a1180acc48ec7755b541079c5e to your computer and use it in GitHub Desktop.
いつからか分かりませんが __builtin_FILE(), __builtin_LINE(), ... は VC++ でも使えるようです
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#pragma message("__cplusplus=" _CRT_STRINGIZE(__cplusplus)) // 「追加のオプション」で /Zc:__cplusplus を足すのを忘れない
#include <locale>
#include <string>
#include <iostream>
#if __cplusplus >= 202002L
#include <source_location>
int wmain()
{
std::locale::global(std::locale(""));
const std::source_location location = std::source_location::current();
std::cout << location.file_name() << '(' << std::to_string(location.line()) << ')' << std::endl;
}
#else
int wmain()
{
std::locale::global(std::locale(""));
std::cout << __builtin_FILE() << '(' << __builtin_LINE() << ')' << std::endl;
}
#endif
@uemuraj
Copy link
Author

uemuraj commented Dec 31, 2021

読んでいたら、こういう定義だったので...いつからなんでしょうね。

struct source_location {
    _NODISCARD static consteval source_location current(const uint_least32_t _Line_ = __builtin_LINE(),
        const uint_least32_t _Column_ = __builtin_COLUMN(), const char* const _File_ = __builtin_FILE(),
        const char* const _Function_ = __builtin_FUNCTION()) noexcept {
        source_location _Result;
        _Result._Line     = _Line_;
        _Result._Column   = _Column_;
        _Result._File     = _File_;
        _Result._Function = _Function_;
        return _Result;
    }

    _NODISCARD_CTOR constexpr source_location() noexcept = default;

    _NODISCARD constexpr uint_least32_t line() const noexcept {
        return _Line;
    }
    _NODISCARD constexpr uint_least32_t column() const noexcept {
        return _Column;
    }
    _NODISCARD constexpr const char* file_name() const noexcept {
        return _File;
    }
    _NODISCARD constexpr const char* function_name() const noexcept {
        return _Function;
    }

private:
    uint_least32_t _Line{};
    uint_least32_t _Column{};
    const char* _File     = "";
    const char* _Function = "";
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment