Search code examples
c++windowsvisual-studio-2017c++17

error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string_view' (or there is no acceptable conversion)


#include <iostream>
#include <string>

void Log(std::string_view message)
{
    std::cout << message << std::endl;
}

int main()
{
    const char* text = "Test";
    Log(text);
    std::cin.get();
}

I am getting that error and #include <string> is included at the start. Any ideas?


Solution

  • Like others already mentioned - std::string_view is defined in the standard header "string_view", which must be included - otherwise string_view is not defined.

    Because you include some headers like "string" and "iostream" which have some connection to std::string_view it is clear that they do at least some forward_declarations.

    In some implementations string_view might be already included in other system headers. For example std::string_view could be implemented/defined in the header "string" and the header "string_view" could just include "string".

    But in general this is an implementation detail of the library implementation. To be able to use std::string_view, it is required to include that header.