4

It's not new that IntelliSense often lags behind C++ development.

For instance, the code below is valid in C++20, using the new Template String-Literal Operator feature.

template<typename C, size_t Size>
struct StrWrapper
{
    std::array<C, Size> m_buf;
    consteval StrWrapper(const C(&str)[Size]) : m_buf{}
    {
        std::copy(str, str + Size, m_buf.begin());
    }
};

template <StrWrapper c>
consteval auto operator ""_wrap()
{
    return c;
}

"hello world"_wrap;

But IntelliSense will report these errors:

E2500   a literal operator template must have a template parameter list equivalent to '<char ...>'
E2486   user-defined literal operator not found

I have found others who have the same problem, there are two reports on Developer Community, the earliest one is from Jan 2021, it's been nearly two years since.

It looks like Microsoft didn't want to solve the issue since this feature is somewhat not used often, and they're still struggling with the modules.

Is there any way to work around this? I've searched for a way to disable specific errors in IntelliSense but there seems to be none. Actually, there is one but it wouldn't help in this case since every single string that uses ""_wrap would have to be in the __INTELLISENSE__ preprocessor block.

thedemons
  • 1,139
  • 2
  • 9
  • 25
  • 2
    Have you tried adding a [template deduction guide](https://godbolt.org/z/v4xhe56M6)? – viraltaco_ Oct 22 '22 at 17:02
  • 2
    @viraltaco_ Thanks but it didn't work in this case, I learned something new tho – thedemons Oct 23 '22 at 03:57
  • @viraltaco_ it doesn't work for me, [here](https://i.imgur.com/Bva0BXB.png) is an image, it compiles just fine but intellisense still complains. –  Oct 23 '22 at 08:44
  • @thedemons Maybe you could provide a dummy implementation just for IntelliSense: `consteval StrWrapper operator""_wrap(const char * s, size_t len) { return StrWrapper(""); }` if `__INTELLISENSE__` is defined. See example on [godbolt](https://godbolt.org/z/zbdc15qW9). Depending on how you use it, IntelliSense might not complain. Is that viable? – Sedenion Oct 23 '22 at 09:52
  • @Sedenion oh yes! Actually, I figured it out and was writing the answer when you commented, but please make that an answer I'll mark it as accepted, thanks! – thedemons Oct 23 '22 at 09:58

1 Answers1

2

Depending on how you use the return value of your operator afterwards, you might satisfy IntelliSense by providing a dummy implementation just for IntelliSense:

template <typename C, size_t Size>
struct StrWrapper
{
  std::array<C, Size> m_buf;
  consteval StrWrapper(const C (&str)[Size])
    : m_buf{}
  {
    std::copy(str, str + Size, m_buf.begin());
  }
};

#ifndef __INTELLISENSE__

template <StrWrapper c>
consteval auto operator""_wrap()
{
  return c;
}

#else

// Dummy, only seen by IntelliSense.
consteval auto operator""_wrap(const char*, size_t) 
{
  return StrWrapper("");
}

#endif

void foo()
{
  constexpr auto t = "hello world"_wrap;
}
thedemons
  • 1,139
  • 2
  • 9
  • 25
Sedenion
  • 5,421
  • 2
  • 14
  • 42