fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
20.6k stars 2.47k forks source link

join couldn't be found an appropriate overload when fmt is imported as a module. #4190

Open LeenHawk opened 4 days ago

LeenHawk commented 4 days ago

The compiler is failing to find an appropriate overload for the fmt::join function that works with std::vector<int> or other std::vector<xxx>.

Environment:

  1. MSVC 17.0
  2. CMake 3.30.2
  3. ninja 1.11.1

Error message:

C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): error C2672: 'fmt::v11::join': no matching overloaded function found
C:\Users\leenhawk\Workspace\stamp2\cmake-build-debug\_deps\fmt-src\include\fmt\xchar.h(157): note: it could be 'fmt::v11::tuple_join_view<wchar_t,T...> fmt::v11::join(const std::tuple<_Types...> &,fmt::v11::basic_string_view<wchar_t>)'
C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): note: 'fmt::v11::tuple_join_view<wchar_t,T...> fmt::v11::join(const std::tuple<_Types...> &,fmt::v11::basic_string_view<wchar_t>)': cannot deduce 'const std::tuple<_Types...> &' from 'const std::vector<std::string,std::allocator<std::string>>'
C:\Users\leenhawk\Workspace\stamp2\cmake-build-debug\_deps\fmt-src\include\fmt\xchar.h(151): note: or 'fmt::v11::join_view<const T*,const T*,wchar_t> fmt::v11::join(std::initializer_list<_Elem>,fmt::v11::basic_string_view<wchar_t>)'
C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): note: 'fmt::v11::join_view<const T*,const T*,wchar_t> fmt::v11::join(std::initializer_list<_Elem>,fmt::v11::basic_string_view<wchar_t>)': cannot deduce 'std::initializer_list<_Elem>' from 'const std::vector<std::string,std::allocator<std::string>>'
C:\Users\leenhawk\Workspace\stamp2\cmake-build-debug\_deps\fmt-src\include\fmt\xchar.h(144): note: or 'fmt::v11::join_view<unknown-type,unknown-type,wchar_t> fmt::v11::join(Range &&,fmt::v11::basic_string_view<wchar_t>)'
C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): note: 'initialization': cannot convert 'const char [3]' to 'fmt::v11::basic_string_view<wchar_t>'
C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): note: 'fmt::v11::basic_string_view<wchar_t>::basic_string_view': no overloaded function can convert all argument types
C:\Users\leenhawk\Workspace\stamp2\cmake-build-debug\_deps\fmt-src\include\fmt\base.h(521): note: it could be 'fmt::v11::basic_string_view<wchar_t>::basic_string_view(const Char *)'
        with
        [
            Char=wchar_t
        ]
C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): note: 'fmt::v11::basic_string_view<wchar_t>::basic_string_view(const Char *)': cannot convert argument 1 from 'const char [3]' to 'const Char *'
        with
        [
            Char=wchar_t
        ]
C:\Users\leenhawk\Workspace\stamp2\main.cpp(137): note: types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

The minimal reproducal example:

CMakeLists.txt

cmake_minimum_required(VERSION 3.30 FATAL_ERROR)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
set(CMAKE_CXX_MODULE_STD 1)

project(main LANGUAGES CXX)

add_executable(main main.cpp)

include(FetchContent)
FetchContent_Declare(
        fmt
        GIT_REPOSITORY https://github.com/fmtlib/fmt
        GIT_TAG        0c9fce2ffefecfdce794e1859584e25877b7b592) # 11.0.2
FetchContent_MakeAvailable(fmt)
#target_link_libraries(main fmt::fmt)
target_sources(main
        PUBLIC FILE_SET CXX_MODULES
        FILES
        ${fmt_SOURCE_DIR}/src/fmt.cc
)
target_include_directories(main PRIVATE ${fmt_SOURCE_DIR}/include)

main.cpp

import fmt;
import std;
int main(int argc, char* argv[])
{
    auto v = std::vector<int>{1, 2, 3};
    fmt::print("{}", fmt::join(v, ", "));
    return 0;
}
LeenHawk commented 4 days ago

There is a example working correctly in goltbot with#include:

#include <fmt/ranges.h>
#include <vector>
#include <string>
int main() {
  auto v = std::vector<std::string>{"1", "2", "3"};
    fmt::print("{}", fmt::join(v, ", "));
}
vitaut commented 4 days ago

Looks like this overload of join is currently not marked as exported: https://github.com/fmtlib/fmt/blob/02537548f3a9efb5f3b83755acf50c8a16ba58c8/include/fmt/ranges.h#L700

A PR to fix it would be welcome.