0

I'm compiling a simple (and possibly erroneous) C++ file:

// file test_function.cpp
#include <functional>

void foo()
{
    void* (*func)() = nullptr;
    std::function<void()> func_(func);
}

I think the type of func_ should be std::function<void*()>. As expected, clang++ with libc++ gives an error:

$ clang++ -std=c++14 -c -stdlib=libc++ test_function.cpp
test_function.cpp:6:27: error: no matching constructor for initialization of 'std::function<void ()>'
    std::function<void()> func_(func);
                          ^     ~~~~

However, g++ and clang++ with libstdc++ gives no errors:

$ clang++ -std=c++14 -c -stdlib=libstdc++ test_function.cpp
$ g++ -std=c++14 -c test_function.cpp

Is the situation normal? Are there descriptions about function type conversion in C++ standards?

Chih-Hsuan Yen
  • 754
  • 2
  • 11
  • 29

1 Answers1

1

I don't know why you think that the type of func should be std::function<void*()>. After all, you defined func_ otherwise, as std::function<void()>. The definition of func_ determines its type.

The error you get is related to the question whether you can ignore the return type of func, the raw function pointer (Your choice of names is rather confusing TBH). This should be possible.

MSalters
  • 173,980
  • 10
  • 155
  • 350