Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error for runtime strings #2536

Closed
ominusliticus opened this issue Oct 8, 2021 · 8 comments
Closed

Compile error for runtime strings #2536

ominusliticus opened this issue Oct 8, 2021 · 8 comments
Labels

Comments

@ominusliticus
Copy link

I am incorporating the fmt library in my project using CMake and link it as a public library.

When I build a C++ project targeted at Visual Studio platform, it's compiler and intellisense complain about this line of code

template <fmt::detail_exported::fixed_string Str>
constexpr auto operator""_a() 
    -> detail::udl_arg<remove_cvref_t<decltype(Str.data[0])>, sizeof(Str.data) / (decltype(Str.data[0])), Str> 
{
  return {};
}

which returns this error:

\fmt\include\fmt\format.h 2986: a literal operator template must have a template parameter list equivalent to '<char ...>'	

The C++ compiler version I am using is C++20. I am not passing any special flags to the compiler.

What do I have to do to fix this error?

@vitaut
Copy link
Contributor

vitaut commented Oct 8, 2021

What compiler version and flags do you use? Could you provide a godbolt repro?

@mwinterb
Copy link
Contributor

mwinterb commented Oct 8, 2021

I never noticed an actual compiler error for this, but I had noticed Intellisense complaining about it. Since it was only a nuisance error listed in the Error List window as I never use the UDLs, I didn't bother reporting it.
Unfortunately, the way to make cl.exe use their EDG front-end (microsoft/STL#1621 (comment)) doesn't work on godbolt. Microsoft seemingly just doesn't include the DLL that Intellisense uses to compile, as cpfe isn't listed in the version info output for either the 32bit or 64bit compiler. The compiler itself handles it fine, though. https://gcc.godbolt.org/z/nT1YvczWs.

I think ICC is the only EDG-based compiler that is on godbolt, but it's using an older version than Microsoft https://gcc.godbolt.org/z/Pdo443dsn (smaller __EDG_VERSION__ number in addition to the C++17 value for __cpp_nontype_template_args), so that doesn't help at all.

It may be enough to add a condition of && !defined(__INTELLISENSE__) when defining FMT_USE_NONTYPE_TEMPLATE_PARAMETERS. @ominusliticus, can you try that to see if that causes any other issues for you? I don't have an environment where I use VS day-to-day anymore.

Full repro output for VS2019 and VS2022:

>cl.exe /std:c++20 /c /BE /Bv fmt_udl.cc
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30136 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Compiler Passes:
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\cl.exe:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1.dll:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1xx.dll:      Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1.dll:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1xx.dll:      Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\VCPackages\cpfe.dll:                      Version 21.00.31727.386
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\VCPackages\cpfe.dll:                      Version 21.00.31727.386
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c2.dll:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1xx.dll:      Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\link.exe:      Version 14.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\mspdb140.dll:  Version 14.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\1033\clui.dll: Version 19.29.30136.0

fmt_udl.cc
"fmt_udl.cc", line 18: error: a literal operator template must have a template
          parameter list equivalent to '<char ...>'
  constexpr auto operator""_a() -> int {
                 ^

"fmt_udl.cc", line 22: error: user-defined literal operator not found
  static_assert("1"_a == 5);
                ^

>cl.exe /std:c++20 /Bv /BE /c fmt_udl.cc
Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30528 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

Compiler Passes:
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\cl.exe:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1.dll:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1xx.dll:      Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1.dll:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1xx.dll:      Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\VC\VCPackages\cpfe.dll:                      Version 22.00.31717.71
 C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\VC\VCPackages\cpfe.dll:                      Version 22.00.31717.71
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c2.dll:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1xx.dll:      Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\link.exe:      Version 14.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\mspdb140.dll:  Version 14.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\1033\clui.dll: Version 19.30.30528.0

fmt_udl.cc
"fmt_udl.cc", line 18: error: a literal operator template must have a template
          parameter list equivalent to '<char ...>'
  constexpr auto operator""_a() -> int {
                 ^

"fmt_udl.cc", line 22: error: user-defined literal operator not found
  static_assert("1"_a == 5);
                ^

And it's not an issue for the normal compiler:

>cl.exe /std:c++20 /c /Bv /DBYPASS_EDG_CHECK /Facon fmt_udl.cc
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30136 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Compiler Passes:
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\cl.exe:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1.dll:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1xx.dll:      Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c2.dll:        Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\c1xx.dll:      Version 19.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\link.exe:      Version 14.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\mspdb140.dll:  Version 14.29.30136.0
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\1033\clui.dll: Version 19.29.30136.0

fmt_udl.cc
; Listing generated by Microsoft (R) Optimizing Compiler Version 19.29.30136.0

        TITLE   C:\Users\mwinterb\source\repos\throwaway\fmt_udl.obj
        .686P
        .XMM
        include listing.inc
        .model  flat

INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES

END

EDG / Intellisense may not even fully implement non-type template parameters and __cpp_nontype_template_args may have been prematurely defined to its C++20 value, since this code causes the compiler to crash if /BE is passed:

static_assert(__cpp_nontype_template_args >= 201911L, "just to get rid of #if's later on");
#ifndef BYPASS_EDG_CHECK
static_assert(__EDG__);
static_assert(__EDG_VERSION__ == 602);
#endif

using size_t = decltype(sizeof(0));

namespace detail_exported {
template <typename Char, size_t N> struct fixed_string {
  constexpr fixed_string(const Char (&)[N]) {
  }
  Char data[N]{};
};
}
template <detail_exported::fixed_string Str>
constexpr auto test_it() -> int {
  return 5;
}
static_assert(test_it<"1">() == 5);
>cl.exe /std:c++20 /c /Bv /BE fmt_udl.cc
Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30528 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

Compiler Passes:
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\cl.exe:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1.dll:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1xx.dll:      Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1.dll:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1xx.dll:      Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\VC\VCPackages\cpfe.dll:                      Version 22.00.31717.71
 C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\VC\VCPackages\cpfe.dll:                      Version 22.00.31717.71
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c2.dll:        Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\c1xx.dll:      Version 19.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\link.exe:      Version 14.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\mspdb140.dll:  Version 14.30.30528.0
 C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\1033\clui.dll: Version 19.30.30528.0

fmt_udl.cc
INTERNAL COMPILER ERROR in 'C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30528\bin\HostX64\x64\cl.exe'
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information

@vitaut
Copy link
Contributor

vitaut commented Oct 8, 2021

Thanks @mwinterb for investigating. A PR to add __INTELLISENSE__ check if it helps would be welcome.

@ominusliticus
Copy link
Author

I replaced all lines

#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS

with

#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS && !defined(__INTELLISENSE__)

and that takes care of all the issues.
Thank you!

@vitaut
Copy link
Contributor

vitaut commented Oct 8, 2021

Closing since this is a bug in intellisense. Please report to Microsoft.

@leewheel
Copy link

leewheel commented Oct 24, 2022

When change "#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS" to "#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS && !defined(INTELLISENSE)" , it's show another error :
Error (active) E0442 too few arguments for class template "fmt::v8::detail::udl_arg"

@cppcooper
Copy link

Closing since this is a bug in intellisense. Please report to Microsoft.

@vitaut are you sure about that? Maybe I'm misunderstanding what that template is actually doing, but it seems like it isn't following the standard and that's why intellisense is listing the error.
See: https://stackoverflow.com/a/53789085/1327399

@vitaut
Copy link
Contributor

vitaut commented May 1, 2023

Yes, I'm sure. That answer is very out of date.

@vitaut vitaut added the invalid label May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants