https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124888

--- Comment #2 from Seb James <sebj_coder3 at pm dot me> ---
I'm seeing the same error in my own code, which can be reduced to this
structure (in this case, the struct with the std::function object is a
singleton):

// main.cpp
import visual;

int main()
{
    Visual v;
    v.call_f();
}

// resources.cpp - a class providing program resources (the f() function) in a
generic way
module;
#include <functional>
export module resources;

export struct Resources
{
private:
    Resources(){}
    ~Resources(){};
public:
    void f() { if (f_impl) { f_impl(); } }
    std::function<void()> f_impl;

    static auto& i()
    {
        static Resources instance;
        return instance;
    }
};

// specific.cpp - a module that gives a specific implementation for f()
module;
#include <iostream>
export module specific;

export struct Specific
{
    static void f() { std::cout << "f() called\n"; }
};

// visual.cpp - a module and class that uses resources
module;

export module visual;

import resources;
import specific;

export struct Visual
{
    Visual() { Resources::i().f_impl = Specific::f; }
    void call_f() { Resources::i().f(); }
};


The output from gcc release-16 branch (or similar for master) is:

[4/6] Building CXX object
bugrep/CMakeFiles/124888_visual.dir/124888_visual.cpp.o
FAILED: bugrep/CMakeFiles/124888_visual.dir/124888_visual.cpp.o
bugrep/CMakeFiles/124888_visual.dir/visual.gcm 
/opt/gcc-16/bin/g++  -I/home/seb/src/mathplot -I/usr/include/hdf5/serial
-I/usr/include/freetype2 -I/home/seb/src/mathplot/maths
-I/home/seb/src/mathplot/json/include
-I/home/seb/src/mathplot/include/rapidxml-1.13 -g -Wall -Wextra -Wpedantic
-pedantic-errors -Werror -Wfatal-errors -Wno-psabi -O3
-fconstexpr-ops-limit=5000000000 -fopenmp
-DMPLOT_FONTS_DIR="\"/home/seb/src/mathplot/fonts\""  -freport-bug -std=gnu++20
-MD -MT bugrep/CMakeFiles/124888_visual.dir/124888_visual.cpp.o -MF
bugrep/CMakeFiles/124888_visual.dir/124888_visual.cpp.o.d -fmodules-ts
-fmodule-mapper=bugrep/CMakeFiles/124888_visual.dir/124888_visual.cpp.o.modmap
-MD -fdeps-format=p1689r5 -x c++ -o
bugrep/CMakeFiles/124888_visual.dir/124888_visual.cpp.o -c
/home/seb/src/mathplot/bugrep/124888_visual.cpp
In file included from /opt/gcc-16/include/c++/16.1.1/functional:76,
                 from /home/seb/src/mathplot/bugrep/124888_resources.cpp:2,
of module resources, imported at
/home/seb/src/mathplot/bugrep/124888_visual.cpp:5:
/opt/gcc-16/include/c++/16.1.1/bits/std_function.h: In instantiation of ‘static
bool std::_Function_handler<_Res(_ArgTypes ...),
_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&,
std::_Manager_operation) [with _Res = void; _Functor = void (*)(); _ArgTypes =
{}]’:
/opt/gcc-16/include/c++/16.1.1/bits/std_function.h:442:21:   required from
‘std::function<_Res(_ArgTypes ...)>::function(_Functor&&) [with _Functor = void
(&)(); _Constraints = void; _Res = void; _ArgTypes = {}]’
  442 |               _M_manager = &_My_handler::_M_manager;
      |                            ^~~~~~~~~~~~~~~~~~~~~~~~
/opt/gcc-16/include/c++/16.1.1/bits/std_function.h:524:4:   required from
‘std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes
...)>::_Callable<_Functor>, std::function<_Res(_ArgTypes ...)>&>
std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = void
(&)(); _Res = void; _ArgTypes = {}; _Requires<_Callable<_Functor>,
std::function<_Res(_ArgTypes ...)>&> = std::function<void()>&; typename
std::enable_if<(!(bool)std::is_same<typename std::remove_cv<typename
std::remove_reference<_Iter>::type>::type, std::function<_Res(_ArgTypes ...)>
>::value), std::decay<_Func> >::type::type = void (*)(); typename
std::enable_if<(!(bool)std::is_same<typename std::remove_cv<typename
std::remove_reference<_Iter>::type>::type, std::function<_Res(_ArgTypes ...)>
>::value), std::decay<_Func> >::type = std::decay<void (&)()>; typename
std::remove_cv<typename std::remove_reference<_Iter>::type>::type = void();
typename std::remove_reference<_Iter>::type = void()]’
  524 |           function(std::forward<_Functor>(__f)).swap(*this);
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/seb/src/mathplot/bugrep/124888_visual.cpp:10:50:   required from here
   10 |     Visual() { Resources::i().f_impl = Specific::f; }
      |                                                  ^
/opt/gcc-16/include/c++/16.1.1/bits/std_function.h:279:53: error: must
‘#include <typeinfo>’ before using ‘typeid’
   69 |    */
  +++ |+#include <typeinfo>
   70 |   template<typename _Tp>
......
  279 |             __dest._M_access<const type_info*>() = &typeid(_Functor);
      |                                                     ^~~~~~~~~~~~~~~~
compilation terminated due to -Wfatal-errors.
ninja: build stopped: subcommand failed.



To reproduce in my *non-reduced* code:

Install some prerequisites for this OpenGL program. On Ubuntu/Debian it's
these:

 sudo apt install build-essential cmake git ninja-build \
                 librapidxml-dev freeglut3-dev libglu1-mesa-dev libxmu-dev \
                 libxi-dev libglfw3-dev libfreetype-dev libhdf5-dev

PLUS g++ compiled from master or release-16 and cmake at least version 3.28.5.

clone mathplot recursively (it has two submodules):

 git clone --recurse-submodules [email protected]:sebsjames/mathplot

switch to the bugs/std_function_typeinfo_before_typeid branch

 cd mathplot
 git checkout bugs/std_function_typeinfo_before_typeid
 git submodule update

Build using cmake:

 mkdir bgcc
 cd bgcc
 CXX=/path/to/g++ cmake .. -GNinja
 ninja helloworld


The reduced example is also found in that branch, in bugrep/124888_*.cpp

Reply via email to