1

I have a small program that compiles on GCC but not on MSVCwhich compiler isn't following the standard for constexpr string_view comparison?

#include <iostream>
#include <string_view>

int main(int argc, char **argv) {
    const constexpr auto a = "z";
    const constexpr std::string_view test("z",1);
    const constexpr std::string_view test2(a,1);
    if constexpr(test == test2) {
        return 5;
    }
    else{
        return 2;
    }
}
Bomaz
  • 1,871
  • 1
  • 17
  • 22
  • 3
    Which version of MSVS are you using? What error(s) do you get? – NathanOliver Apr 11 '18 at 13:38
  • 1
    Apparently it will only compile with GCC 7.3 and not with the earlier versions. – Ron Apr 11 '18 at 13:42
  • I'm unfamiliar with MSVC versioning but from Compiler Explorer it looks like that version doesn't support `if constexpr`. [This](https://blogs.msdn.microsoft.com/vcblog/2017/11/15/msvc-conformance-improvements-in-visual-studio-2017-version-15-5/) page details C++17 features and says "These features can be enabled by using the /std:c++17 version switch." If I add that switch on Compiler Explorer it tells me it's an unknown option. Have you tried running it locally with the latest MSVC? – Ellis Apr 11 '18 at 13:49

1 Answers1

3

C++17 constexpr if statements are supported since MSVC 19.11.

We can see in the error message that Compiler Explorer currently uses version 19.10.25017.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • removing the constexpr from the if statement and making a constexpr bool doesn't fix it however https://godbolt.org/g/JhwL2W – Bomaz Apr 11 '18 at 14:14
  • 2
    @Bomaz There appears to be a bug in MSVS's implementation of the string comparison. You should report it. – NathanOliver Apr 11 '18 at 14:16
  • 1
    @NathanOliver https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html – Bomaz Apr 11 '18 at 15:24