0

The following c++ code compiles fine with MSVC but doesn't compile with CLang.

void Test()
{
    auto str = __FUNCTION__ "." "Description";
}

why does this work for MSVC only? how do I make this work for clang? (. and Description can append without any issues in both compilers)

clang error: expected ';' at end of declaration

Note that "Test . Description" is not a solution, I want to use this in a macro and make static strings based on what function the macro is used in.

9
  • What version of CLang? What version of C++ did you tell CLang to use? Commented Nov 13, 2024 at 18:44
  • 1
    To be clear, that's not standard C++ code. The name __FUNCTION__, because it contains two consecutive underscores, is reserved for use by the implementation. Whatever it does is up to the compiler writer. So consult your compiler's documentation. Commented Nov 13, 2024 at 18:46
  • related/dupe: stackoverflow.com/questions/18298829/… Commented Nov 13, 2024 at 18:46
  • @ThomasMatthews here is godbolt demonstration godbolt.org/z/8T8G871G4 I use the latest version Commented Nov 13, 2024 at 18:47
  • 3
    As of C++ 20, std::source_location Commented Nov 13, 2024 at 18:57

1 Answer 1

0

If clang is run with -E, it can be seen that __FUNCTION__ is not a macro. This also was noticed in the comment with the linked answer: In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.

# 1 "/app/example.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 468 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/app/example.cpp" 2

void Test()
{
    auto str = __FUNCTION__ ".Description";
}

You can make it like

#include <string>
using namespace std::string_literals;

void Test() {
  const auto str = __FUNCTION__ + ".Description"s;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.