Skip to content

No matching operator new when instanciating a template method from another module #59601

Closed
@shangoue

Description

When trying to migrate my application to C++ modules using clang-15 on ubuntu 22.04, I encounter errors with template resolution.
Maybe be this is linked to #56916 but the last comment says it all about grammar sugar.

Please find below a minimal error case to reproduce my problem.

First, a module defines a template method using an unordered_map:

module;
#include <unordered_map>
export module test;
export namespace test
{
    struct S
    {
        std::unordered_map<int,int> map;
        template <typename T> void d()
        {
            map.try_emplace(0, 0);
        }
    };
}

Then, a second one calls it:

export module test2;
import test;
export namespace test
{
    void f(S & s)
    {
        s.d<int>();
    }
}

When compiling the second module, I got errors:

$ clang++-15 --precompile test.cppm -o test.pcm -std=c++20 -fprebuilt-module-path=. -Wall -I.
$ clang++-15 --precompile test2.cppm -o test2.pcm -std=c++20 -fprebuilt-module-path=. -Wall -I.
In module 'test' imported from test2.cppm:2:
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/hashtable_policy.h:1965:6: error: no matching 'operator new' function for non-allocating placement new expression; include <new>
            ::new ((void*)__n) __node_type;
            ^~
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/hashtable.h:306:19: note: in instantiation of function template specialization 'std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const int, int>, false>>>::_M_allocate_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<int &&>>' requested here
            _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
                         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/hashtable_policy.h:912:39: note: in instantiation of function template specialization 'std::_Hashtable<int, std::pair<const int, int>, std::allocator<std::pair<const int, int>>, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true>>::_Scoped_node::_Scoped_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<int &&>>' requested here
          typename __hashtable::_Scoped_node __node {
                                             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/unordered_map.h:481:16: note: in instantiation of function template specialization 'std::__detail::_Insert_base<int, std::pair<const int, int>, std::allocator<std::pair<const int, int>>, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true>>::try_emplace<int, int>' requested here
          return _M_h.try_emplace(cend(), std::move(__k),
                      ^
/home/etham/test_modules/test.cppm:14:17: note: in instantiation of function template specialization 'std::unordered_map<int, int>::try_emplace<int>' requested here
            map.try_emplace(0, 0);
                ^
test2.cppm:7:11: note: in instantiation of function template specialization 'test::S::d<int>' requested here
        s.d<int>();
          ^
In module 'test' imported from test2.cppm:2:
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/alloc_traits.h:518:4: error: no matching function for call to 'construct_at'
          std::construct_at(__p, std::forward<_Args>(__args)...);
          ^~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/hashtable_policy.h:1966:27: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::__detail::_Hash_node<std::pair<const int, int>, false>>>::construct<std::pair<const int, int>, const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<int &&>>' requested here
            __node_alloc_traits::construct(_M_node_allocator(),
                                 ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/hashtable.h:306:19: note: in instantiation of function template specialization 'std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const int, int>, false>>>::_M_allocate_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<int &&>>' requested here
            _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
                         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/hashtable_policy.h:912:39: note: in instantiation of function template specialization 'std::_Hashtable<int, std::pair<const int, int>, std::allocator<std::pair<const int, int>>, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true>>::_Scoped_node::_Scoped_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<int &&>>' requested here
          typename __hashtable::_Scoped_node __node {
                                             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/unordered_map.h:481:16: note: in instantiation of function template specialization 'std::__detail::_Insert_base<int, std::pair<const int, int>, std::allocator<std::pair<const int, int>>, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true>>::try_emplace<int, int>' requested here
          return _M_h.try_emplace(cend(), std::move(__k),
                      ^
/home/etham/test_modules/test.cppm:14:17: note: in instantiation of function template specialization 'std::unordered_map<int, int>::try_emplace<int>' requested here
            map.try_emplace(0, 0);
                ^
test2.cppm:7:11: note: in instantiation of function template specialization 'test::S::d<int>' requested here
        s.d<int>();
          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/stl_construct.h:94:5: note: candidate template ignored: substitution failure [with _Tp = std::pair<const int, int>, _Args = <const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<int &&>>]: no matching 'operator new' function for non-allocating placement new expression; include <new>
    construct_at(_Tp* __location, _Args&&... __args)
    ^

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"clang:modulesC++20 modules and Clang Header Modules

Type

No type

Projects

  • Status

    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions