Bug 107580 - std::vector<std::string> parameter cannot be inferred with -D_GLIBCXX_USE_CXX11_ABI=0
Summary: std::vector<std::string> parameter cannot be inferred with -D_GLIBCXX_USE_CXX...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ABI
Depends on:
Blocks:
 
Reported: 2022-11-08 19:57 UTC by Vyas Ramasubramani
Modified: 2022-11-09 18:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-11-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vyas Ramasubramani 2022-11-08 19:57:11 UTC
The type of the argument to the constructor of A cannot be correctly inferred when using the old ABI:

#include <string>
#include <vector>
struct A {
    A(std::string const& file_path) {}
    A(std::vector<std::string> const& file_path) {}
};

int main() {
    std::string x = "hello";
    std::string y = "goodbye";
    // Both of the below will fail.
    A a{{x, y}};
    A a = A({x, y});
}

Here's the output from running the compilation:

(test_gcc) vyasr-dt% g++ test.cpp -D_GLIBCXX_USE_CXX11_ABI=0
test.cpp: In function ‘int main()’:
test.cpp:11:16: error: call of overloaded ‘A(<brace-enclosed initializer list>)’ is ambiguous
   11 |     A a1{{x, y}};
      |                ^
test.cpp:5:5: note: candidate: ‘A::A(const std::vector<std::basic_string<char> >&)’
    5 |     A(std::vector<std::string> const& file_path) {}
      |     ^
test.cpp:4:5: note: candidate: ‘A::A(const string&)’
    4 |     A(std::string const& file_path) {}
      |     ^
test.cpp:12:20: error: call of overloaded ‘A(<brace-enclosed initializer list>)’ is ambiguous
   12 |     A a2 = A({x, y});
      |                    ^
test.cpp:5:5: note: candidate: ‘A::A(const std::vector<std::basic_string<char> >&)’
    5 |     A(std::vector<std::string> const& file_path) {}
      |     ^
test.cpp:4:5: note: candidate: ‘A::A(const string&)’
    4 |     A(std::string const& file_path) {}
      |     ^

And here is my current version information:

(test_gcc) vyasr-dt% g++ -v       
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-Av3uEd/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 


Here is an example using the Compiler Explorer showing that this issue is specific to libstdc++ and the old ABI flag: https://godbolt.org/z/jqoc6j3be. For instance, with clang the error appears unless `-stdlib=libc++` is specified, at which point the code compiles successfully.
Comment 1 Andrew Pinski 2022-11-08 22:21:30 UTC
I think this is expected this is why there was a need for the new implementation of std::string and why there was a new ABI. the old std::string is not C++11 compatiable at all. I suspect this is a won't fix.
Comment 2 Jonathan Wakely 2022-11-09 04:30:33 UTC
This is actually trivial to fix:

--- a/libstdc++-v3/include/bits/cow_string.h
+++ b/libstdc++-v3/include/bits/cow_string.h
@@ -680,7 +680,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @param  __end  End of range.
        *  @param  __a  Allocator to use (default is default allocator).
        */
-      template<class _InputIterator>
+#if __cplusplus >= 201103L
+      template<typename _InputIterator,
+              typename = std::_RequireInputIter<_InputIterator>>
+#else
+      template<typename _InputIterator>
+#endif
        basic_string(_InputIterator __beg, _InputIterator __end,
                     const _Alloc& __a = _Alloc())
        : _M_dataplus(_S_construct(__beg, __end, __a), __a)
Comment 3 Jonathan Wakely 2022-11-09 18:54:33 UTC
Unfortunately this changes the mangled name of the function, and so breaks the library ABI. Maybe that's why I didn't do it sooner.

There are ways to solve that, but it's not as trivial as I thought.