bool HuffmanNode::Compare::operator()(const HuffmanNode &n1, const HuffmanNode &n2) const
{
if (n1.frequency == n2.frequency) {
return lessThan ? n1.character < n2.character : n1.character >= n2.character;
} else {
return lessThan ? n1.frequency < n2.frequency : n1.frequency >= n2.frequency;
}
}
bool HuffmanNode::Compare::operator()(const HuffmanNode *n1, const HuffmanNode *n2) const
{
return operator()(*n1, *n2);
}
Here I have a compare function from a compare object that is within another object (HuffmanNode).
I want to pass the compare function to another object through a template:
HeapQueue<HuffmanNode, HuffmanNode::Compare> queue;
This doesn't seem to work and I've even tried using the operator function.
What is the proper format for this? Thanks.
Here is my compiler error
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1826:31: error:
no matching constructor for initialization of 'HuffmanNode'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1718:18: note:
in instantiation of function template specialization
'std::__1::allocator<HuffmanNode>::construct<HuffmanNode>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1561:14: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::__construct<HuffmanNode>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1030:25: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::construct<HuffmanNode>' requested here
__alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1121:9: note:
in instantiation of member function 'std::__1::vector<HuffmanNode,
std::__1::allocator<HuffmanNode> >::__construct_at_end' requested here
__construct_at_end(__n);
^
./HeapQueue.hpp:25:26: note: in instantiation of member function
'std::__1::vector<HuffmanNode, std::__1::allocator<HuffmanNode> >::vector'
requested here
VectorCompleteTree() : V(1) {}
^
./HeapQueue.hpp:46:7: note: in instantiation of member function
'VectorCompleteTree<HuffmanNode>::VectorCompleteTree' requested here
class HeapQueue
^
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit copy
constructor) not viable: requires 1 argument, but 0 were provided
class HuffmanNode {
^
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit move
constructor) not viable: requires 1 argument, but 0 were provided
./HuffmanBase.hpp:11:3: note: candidate constructor not viable: requires 2
arguments, but 0 were provided
HuffmanNode(char c, size_t f) : HuffmanNode(c, f, nullptr, nullptr, nu...
^
./HuffmanBase.hpp:10:3: note: candidate constructor not viable: requires 5
arguments, but 0 were provided
HuffmanNode(char c, size_t f, HuffmanNode *p, HuffmanNode *l, HuffmanN...
^
1 error generated.
Starting from the bottom of the error message, the compiler is trying to do this HuffmanNode{} but failing because HuffmanNode doesn't have a default constructor. The key here is but 0 were provided.
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit copy
constructor) not viable: requires 1 argument, but 0 were provided
class HuffmanNode {
^
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit move
constructor) not viable: requires 1 argument, but 0 were provided
./HuffmanBase.hpp:11:3: note: candidate constructor not viable: requires 2
arguments, but 0 were provided
HuffmanNode(char c, size_t f) : HuffmanNode(c, f, nullptr, nullptr, nu...
^
./HuffmanBase.hpp:10:3: note: candidate constructor not viable: requires 5
arguments, but 0 were provided
HuffmanNode(char c, size_t f, HuffmanNode *p, HuffmanNode *l, HuffmanN...
^
The default constructor of HeapQueue is invoking the default constructor of VectorCompleteTree<HuffmanNode>.
./HeapQueue.hpp:46:7: note: in instantiation of member function
'VectorCompleteTree<HuffmanNode>::VectorCompleteTree' requested here
class HeapQueue
The default constructor of VectorCompleteTree<HuffmanNode> is constructing a std::vector<HuffmanNode> with one element. This one element needs to be default constructed.
./HeapQueue.hpp:25:26: note: in instantiation of member function
'std::__1::vector<HuffmanNode, std::__1::allocator<HuffmanNode> >::vector'
requested here
VectorCompleteTree() : V(1) {}
std::vector<HuffmanNode> is trying to default construct a HuffmanNode but can't.
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1826:31: error:
no matching constructor for initialization of 'HuffmanNode'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1718:18: note:
in instantiation of function template specialization
'std::__1::allocator<HuffmanNode>::construct<HuffmanNode>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1561:14: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::__construct<HuffmanNode>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1030:25: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::construct<HuffmanNode>' requested here
__alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1121:9: note:
in instantiation of member function 'std::__1::vector<HuffmanNode,
std::__1::allocator<HuffmanNode> >::__construct_at_end' requested here
__construct_at_end(__n);
It's all there in the error message! The simple solution would be do give HuffmanNode a default constructor.
Related
So I have an issue in converting between a base and derived class after adding some code to (de)serialize for JSON. I am using the nlohmann JSON library. For background, here is the general original set up (before the JSON code):
class Base;
class Derived : public Base;
std::unique_ptr<Derived> Foo(Node x) {
std::unique_ptr<Derived> result;
/* Set some fields */
return result;
}
std::unique_ptr<Base> NodeToBase(Node x) {
std::unique_ptr<Base> result = nullptr;
switch (x->type):
case (SomeType):
result = Foo(x);
return result;
}
Before adding the changes, the following assertions worked:
static_assert(std::is_convertible<Derived*, Base*>::value);
static_assert(std::is_convertible<std::unique_ptr<Derived>, std::unique_ptr<Base>>::value);
Next, I added some serialization functions to my base and derived classes as such:
class Base {
virtual nlohmann::json ToJson() const;
virtual void FromJson(const nlohmann::json &j);
}
class Derived : public Base {
nlohmann::json ToJson() const override;
void FromJson(const nlohmann::json &j) override;
}
I also removed any const quantifiers on member variables in Base and Derived to allow for deserialization.
And now, after compiling, the static_assert's above no longer worked, and I get the following error.
foo.cpp:155:14: error: no viable overloaded '='
result = Foo(x);
~~~~~~ ^ ~~~~~~~
/usr/local/Cellar/llvm#6/6.0.1/include/c++/v1/memory:2347:28: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unique_ptr<Derived, default_delete<Derived>>' to 'const unique_ptr<Base, default_delete<Base>>' for 1st argument
class _LIBCPP_TEMPLATE_VIS unique_ptr {
^
/usr/local/Cellar/llvm#6/6.0.1/include/c++/v1/memory:2463:15: note: candidate function not viable: no known conversion from 'unique_ptr<Derived, default_delete<Derived>>' to 'unique_ptr<Base, default_delete<Base>>' for 1st argument
unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
^
/usr/local/Cellar/llvm#6/6.0.1/include/c++/v1/memory:2555:15: note: candidate function not viable: no known conversion from 'std::unique_ptr<Derived>' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
unique_ptr& operator=(nullptr_t) _NOEXCEPT {
^
/usr/local/Cellar/llvm#6/6.0.1/include/c++/v1/memory:2474:15: note: candidate template ignored: requirement 'is_convertible<typename unique_ptr<Derived, default_delete<Derived> >::pointer, pointer>::value' was not satisfied [with _Up = Derived, _Ep = std::__1::default_delete<Derived>]
unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
^
Any help would be appreciated. The only workaround I've found that works is to replace:
result = Foo(x);
with
result = std::unique_ptr<Base>(reinterpret_cast<Base *>(Foo(x).release()));
I'm trying to store my methodDecl nodes in a map within RecursiveASTVisitor
std::map<std::string, ObjCMethodDecl> ObjCMethodsMap;
Currently I'm storing it like this
virtual bool HandleTopLevelDecl (DeclGroupRef DG) {
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
Decl *D = *i;
if (ObjCMethodDecl *methodDecl = dyn_cast<ObjCMethodDecl>(D)) {
std::string methodName = methodDecl->getNameAsString();
ObjCMethodsMap[methodName] = ObjCMethodDecl(*methodDecl);
}
}
}
And when I'm trying to use it, I do this
ObjCMethodDecl methodDecl = ObjCMethodsMap["textFieldShouldReturn:"];
Visitor->TraverseDecl(&methodDecl);
This may be more of a C++ question, but am I doing anything wrongly in my way of storing it in reference?
Below is the compilation error message I'm getting. I'm not sure what does it say now because it is referencing to something not within my code and doesn't tell me which line it came from. That's why I thought maybe I did something wrong in C++
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1645:31: error: no matching constructor for initialization of 'clang::ObjCMethodDecl'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1572:18: note: in instantiation of function template specialization 'std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char>, clang::ObjCMethodDecl>, void *> >::construct<clang::ObjCMethodDecl>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1453:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char>, clang::ObjCMethodDecl>, void *> > >::__construct<clang::ObjCMethodDecl>' requested here
{__construct(__has_construct<allocator_type, pointer, _Args...>(),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1366:20: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char>, clang::ObjCMethodDecl>, void *> > >::construct<clang::ObjCMethodDecl>' requested here
__node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.second));
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1380:29: note: in instantiation of member function 'std::__1::map<std::__1::basic_string<char>, clang::ObjCMethodDecl, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, clang::ObjCMethodDecl> > >::__construct_node_with_key' requested here
__node_holder __h = __construct_node_with_key(__k);
^
/Users/jeremy/Desktop/clang-llvm/llvm/tools/clang/tools/extra/myASTChecker/MyASTChecker.cpp:272:55: note: in instantiation of member function 'std::__1::map<std::__1::basic_string<char>, clang::ObjCMethodDecl, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, clang::ObjCMethodDecl> > >::operator[]' requested here
ObjCMethodDecl methodDecl = ObjCMethodsMap[selectorName];
^
/Users/jeremy/Desktop/clang-llvm/llvm/tools/clang/include/clang/AST/DeclObjC.h:113:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class ObjCMethodDecl : public NamedDecl, public DeclContext {
^
/Users/jeremy/Desktop/clang-llvm/llvm/tools/clang/include/clang/AST/DeclObjC.h:113:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
/Users/jeremy/Desktop/clang-llvm/llvm/tools/clang/include/clang/AST/DeclObjC.h:226:3: note: candidate constructor not viable: requires at least 6 arguments, but 0 were provided
ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
I am currently using the cpprest API found at https://casablanca.codeplex.com/ for a simple RESTful API project.
This popular library uses Microsoft's PPLX framework to facilitate the async tasks.
I am currently encountering an issue in which code does not compile when a function returns an object of type pplx::task<SomeObj> and SomeObj has no default constructor. My understanding is that this code should be a valid as no default objects should be created.
Is there an error in my code or is this a bug?
Example Code:
#include <vector>
#include <string>
#include <cpprest/http_client.h>
class SomeSubObj {
public:
int a,b,c;
SomeSubObj(int a, int b, int c): a(a), b(b), c(c){}
};
class SomeObj {
public:
std::vector<SomeSubObj> subObjs;
SomeObj(std::string json){
// Populate from JSON...
subObjs.push_back(SomeSubObj(1,1,1));
subObjs.push_back(SomeSubObj(2,2,2));
subObjs.push_back(SomeSubObj(3,3,3));
}
};
pplx::task<SomeObj> doSomething(){
return pplx::task<std::string>([]() {
// Make a call out and get some json...
return std::string("{...}");
}).then([](std::string x) {
// Return a transformed object
return SomeObj("...");
});
}
int main()
{
doSomething();
return 0;
}
I am currently receiving the following output:
g++ -std=c++0x -Iboost_thread-mt -Iboost_atomic -Iboost_system -Icpprest -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Bug.d" -MT"src/Bug.d" -o "src/Bug.o" "../src/Bug.cpp"
In file included from ../src/Bug.cpp:4:
In file included from /usr/local/include/cpprest/http_client.h:44:
/usr/local/include/pplx/pplxtasks.h:2404:9: error: call to implicitly-deleted default constructor of '_ResultHolder<SomeObj>'
_Task_impl(_CancellationTokenState * _Ct, scheduler_ptr _Scheduler_arg)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2096:15: note: in instantiation of member function 'pplx::details::_Task_impl<SomeObj>::_Task_impl' requested here
__second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2359:15: note: in instantiation of function template specialization 'std::__1::__libcpp_compressed_pair_imp<std::__1::allocator<pplx::details::_Task_impl<SomeObj> >, pplx::details::_Task_impl<SomeObj>, 1>::__libcpp_compressed_pair_imp<std::__1::allocator<pplx::details::_Task_impl<SomeObj> > &, pplx::details::_CancellationTokenState *&, pplx::scheduler_ptr &, 0, 0, 1>' requested here
: base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:3697:16: note: in instantiation of function template specialization 'std::__1::__compressed_pair<std::__1::allocator<pplx::details::_Task_impl<SomeObj> >, pplx::details::_Task_impl<SomeObj> >::__compressed_pair<std::__1::allocator<pplx::details::_Task_impl<SomeObj> > &, pplx::details::_CancellationTokenState *&, pplx::scheduler_ptr &>' requested here
: __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4271:26: note: in instantiation of function template specialization 'std::__1::__shared_ptr_emplace<pplx::details::_Task_impl<SomeObj>, std::__1::allocator<pplx::details::_Task_impl<SomeObj> > >::__shared_ptr_emplace<pplx::details::_CancellationTokenState *&, pplx::scheduler_ptr &>' requested here
::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4630:29: note: in instantiation of function template specialization 'std::__1::shared_ptr<pplx::details::_Task_impl<SomeObj> >::make_shared<pplx::details::_CancellationTokenState *&, pplx::scheduler_ptr &>' requested here
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
^
/usr/local/include/pplx/pplxtasks.h:1429:103: note: (skipping 1 context in backtrace; use -ftemplate-backtrace-limit=0 to see all)
static _Type _Make(_CancellationTokenState * _Ct, scheduler_ptr _Scheduler_arg) { return std::make_shared<_Task_impl<_ReturnType>>(_Ct, _Scheduler_arg); }
^
/usr/local/include/pplx/pplxtasks.h:3623:52: note: in instantiation of member function 'pplx::details::_Task_ptr<SomeObj>::_Make' requested here
_M_Impl = details::_Task_ptr<_ReturnType>::_Make(_Ct, _Scheduler);
^
/usr/local/include/pplx/pplxtasks.h:4138:27: note: in instantiation of member function 'pplx::task<SomeObj>::_CreateImpl' requested here
_ContinuationTask._CreateImpl(_PTokenState, _Scheduler);
^
/usr/local/include/pplx/pplxtasks.h:4101:16: note: in instantiation of function template specialization 'pplx::task<std::__1::basic_string<char> >::_ThenImpl<std::__1::basic_string<char>, <lambda at ../src/Bug.cpp:27:10> >' requested here
return _ThenImpl<_InternalReturnType, _Function>(_Func, _PTokenState, _TaskOptions.get_continuation_context(), _Scheduler, _CreationStack);
^
/usr/local/include/pplx/pplxtasks.h:3424:16: note: in instantiation of function template specialization 'pplx::task<std::__1::basic_string<char> >::_ThenImpl<std::__1::basic_string<char>, <lambda at ../src/Bug.cpp:27:10> >' requested here
return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions);
^
../src/Bug.cpp:27:5: note: in instantiation of function template specialization 'pplx::task<std::__1::basic_string<char> >::then<<lambda at ../src/Bug.cpp:27:10> >' requested here
}).then([](std::string x) {
^
/usr/local/include/pplx/pplxtasks.h:844:15: note: default constructor of '_ResultHolder<SomeObj>' is implicitly deleted because field '_Result' has no default constructor
_Type _Result;
^
1 error generated.
make: *** [src/Bug.o] Error 1
I have this simplified code consisting of a class with a static function, which is stored in map:
#include <iostream>
#include <functional>
#include <map>
class A {
public:
static void f(const std::string &s) { std::cout << s; }
};
std::map<std::string, std::function<void(std::string const &)>> fs;
int main() {
fs["f"] = &A::f;
fs["f"]("hello");
}
This prints the expected hello.
The problem occurs if I overload f() with:
static void f(const std::string &s, int c) { while(c-->0) { std::cout << s; } }
This results in the error:
error: no viable overloaded '='
fs["f"] = &A::f;
~~~~~~~ ^ ~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2241:7: note: candidate function not viable: no overload of 'f' matching 'const std::function<void (const std::basic_string<char> &)>' for 1st argument
operator=(const function& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2259:7: note: candidate function not viable: no overload of 'f' matching 'std::function<void (const std::basic_string<char> &)>' for 1st argument
operator=(function&& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2273:7: note: candidate function not viable: no overload of 'f' matching 'nullptr_t' for 1st argument
operator=(nullptr_t)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2302:2: note: candidate template ignored: couldn't infer template argument '_Functor'
operator=(_Functor&& __f)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2311:2: note: candidate template ignored: couldn't infer template argument '_Functor'
operator=(reference_wrapper<_Functor> __f) noexcept
^
However, calling both functions works:
A::f("hello ");
A::f("echo ", 3);
So, my question are:
Why this code not compiling even though the operator= seems to exist and function if I don't overload f()?
How can I get it to work without giving both functions different names?
Why this code not compiling even though the operator= seems to exist
and function if I don't overload f()?
Because the compiler doesn't know which overload to choose. How could he? There is no criterion upon which he can decide which one is suited better. Every std::function allows arbitrary function objects to be assigned and doesn't check any signatures. If you wanted to save only function pointers of this particular signature you should have declared the map appropriately.
How can I get it to work without giving both functions different
names?
As already mentioned it works by casting the expression to a function pointer of the specific type.
fs["f"] = static_cast<void(*)(std::string const&)>( &A::f );
This way no ambiguities arise; There is exactly one overload that can be casted to this function to pointer type. If this appears more often then a typedef could be feasible.
Or a little helper class template:
template <typename... Exact>
struct funptr
{
template <typename R>
constexpr auto operator()(R(*p)(Exact...)) -> decltype(p)
{ return p; }
};
fs["f"] = funptr<std::string const&>()(&A::f);
Demo.
The following code doesn't compile:
#include <functional>
struct X
{
std::function<X()> _gen;
};
int main()
{
X x;
x._gen = [] { return X(); }; //this line is causing problem!
}
I don't understand why assignment to x._gen is causing problem. Both gcc and clang are giving similar error messages. Could anyone please explain it?
Compiler error messages
GCC's error:
In file included from main.cpp:1:0:
/usr/include/c++/4.8/functional: In instantiation of ‘std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes ...)>::_CheckResult<std::function<_Res(_ArgTypes ...)>::_Invoke<_Functor>, _Res>, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = main()::__lambda0; _Res = X; _ArgTypes = {}; std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes ...)>::_CheckResult<std::function<_Res(_ArgTypes ...)>::_Invoke<_Functor>, _Res>, std::function<_Res(_ArgTypes ...)>&> = std::function<X()>&]’:
main.cpp:11:12: required from here
/usr/include/c++/4.8/functional:2333:4: error: no matching function for call to ‘std::function<X()>::function(main()::__lambda0)’
function(std::forward<_Functor>(__f)).swap(*this);
^
/usr/include/c++/4.8/functional:2333:4: note: candidates are:
/usr/include/c++/4.8/functional:2255:2: note: template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor)
function(_Functor);
^
/usr/include/c++/4.8/functional:2255:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/functional:2230:7: note: std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = X; _ArgTypes = {}]
function(function&& __x) : _Function_base()
^
/usr/include/c++/4.8/functional:2230:7: note: no known conversion for argument 1 from ‘main()::__lambda0’ to ‘std::function<X()>&&’
/usr/include/c++/4.8/functional:2433:5: note: std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = X; _ArgTypes = {}]
function<_Res(_ArgTypes...)>::
^
/usr/include/c++/4.8/functional:2433:5: note: no known conversion for argument 1 from ‘main()::__lambda0’ to ‘const std::function<X()>&’
/usr/include/c++/4.8/functional:2210:7: note: std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = X; _ArgTypes = {}; std::nullptr_t = std::nullptr_t]
function(nullptr_t) noexcept
^
/usr/include/c++/4.8/functional:2210:7: note: no known conversion for argument 1 from ‘main()::__lambda0’ to ‘std::nullptr_t’
/usr/include/c++/4.8/functional:2203:7: note: std::function<_Res(_ArgTypes ...)>::function() [with _Res = X; _ArgTypes = {}]
function() noexcept
^
/usr/include/c++/4.8/functional:2203:7: note: candidate expects 0 arguments, 1 provided
Likewise, Clang throws this:
main.cpp:11:12: error: no viable overloaded '='
x._gen = [] { return X(); };
~~~~~~ ^ ~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2270:7: note: candidate function not viable: no known conversion from '<lambda at main.cpp:11:14>' to 'const std::function<X ()>' for 1st argument
operator=(const function& __x)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2288:7: note: candidate function not viable: no known conversion from '<lambda at main.cpp:11:14>' to 'std::function<X ()>' for 1st argument
operator=(function&& __x)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2302:7: note: candidate function not viable: no known conversion from '<lambda at main.cpp:11:14>' to 'nullptr_t' for 1st argument
operator=(nullptr_t)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2192:39: note: candidate template ignored: disabled by 'enable_if' [with _Functor = <lambda at main.cpp:11:14>]
using _Requires = typename enable_if<_Cond::value, _Tp>::type;
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2340:2: note: candidate template ignored: could not match 'reference_wrapper<type-parameter-0-0>' against '<lambda at main.cpp:11:14>'
operator=(reference_wrapper<_Functor> __f) noexcept
^
This was PR60594, which got fixed in GCC 4.8.3. The comments on that bug point out why it is valid: although the standard requires template arguments for standard library templates to be a complete type (with some exceptions), X() is a complete type even if X is not.
There are several members of std::function<X()> that do implicitly require X to be a complete type. The template constructor you're using is one of them: it requires the return type of your lambda to be implicitly convertible to X, but whether X is convertible to itself depends on whether X is a complete type: if it's incomplete, the compiler can't rule out the possibility that it is an uncopyable unmovable type.
This requirement follows from:
20.9.11.2.1 function construct/copy/destroy [func.wrap.func.con]
8 Remarks: These constructors shall not participate in overload resolution unless f is Callable (20.9.11.2) for argument types ArgTypes... and return type R.
20.9.11.2 Class template function [func.wrap.func]
2 A callable object f of type F is Callable for argument types ArgTypes and return type R if the expression INVOKE(f, declval<ArgTypes>()..., R), considered as an unevaluated operand (Clause 5), is well formed (20.9.2).
20.9.2 Requirements [func.require]
2 Define INVOKE(f, t1, t2, ..., tN, R) as INVOKE(f, t1, t2, ..., tN) implicitly converted to R.
Several other members of std::function also require X to be a complete type.
You're only using that constructor after type X has already completed, though, so there's no problem: at that point, X certainly can be implicitly converted to X.
The problem was that std::function was performing checks that depend on X being a complete type, in a context where the standard doesn't support performing such checks, and this did not account for the possibility that X would become a complete type after the instantiation of std::function<X()> had already completed.
This may be a gcc bug, but maybe not. It isn't directly in = but rather in the conversion constructor for std::function (which the operator= invokes).
Here is a pathological example of it happening:
#include <iostream>
#include <functional>
struct X
{
std::function<X()> _gen;
};
X func() {return {};};
int main()
{
std::function<X()> foo1( &func ); // compiles
X unused = X{}; // copy ctor invoked
std::function<X()> foo2( &func ); // does not compile!
}
note that the first foo1 works fine, it is not until I cause some code somewhere to invoke the copy ctor that the second one generates errors. Even auto unused =[]{ return X{}; }; is enough. (func direct constructs and never copies).
It is the use/"creation" of the copy ctor that seems to cause the problem.
#include <iostream>
#include <functional>
struct X
{
std::function<X()> _gen;
X( X const& ) = default;
X() = default;
};
X func() {return {};};
int main()
{
std::function<X()> foo1( &func ); // does not compile
}
that copy constructor ends up calling the copy ctor of _gen, possibly before X is a complete type.
If we explicitly delay instantiation of X::X(X const&) until X is a complete type:
#include <functional>
struct X
{
std::function<X()> _gen;
X( X const& );
X() {}
};
X::X( X const& o ):_gen(o._gen){} // or =default *here*
X func() {return {};};
int main()
{
std::function<X()> foo1( &func ); // compiles!
[]{ return X{}; }; // or X unused = X{};
std::function<X()> foo2( &func ); // compiles!
}
the problem goes away.
I suspect that the implicit copy constructor of X created in the body of X when X is an incomplete type implicitly invokes std::function<X()>'s copy constructor, which is in a context where X is incomplete, which breaks the preconditions of its copy constructor being invoked (at least in practice in how it was implemented in gcc -- by the standard? I am unsure.)
By explicitly making a copy ctor outside of X I avoid this, and everything works.
So as a work around to your problem, declare and implement X::X(X const&) outside of X, and the magic error goes away.