I've got a type IVector3 provided by the glm library:
using IVector3 = glm::ivec3;
I've got a hash function for IVector3s:
struct IVector3Hash
{
std::size_t operator()(IVector3 const& i) const noexcept
{
std::size_t seed = 0;
boost::hash_combine(seed, i.x);
boost::hash_combine(seed, i.y);
boost::hash_combine(seed, i.z);
return seed;
}
};
And I'm trying to map IVector3s to floats in an unordered_map:
std::unordered_map<IVector3, float, IVector3Hash> g_score;
However, when I try and emplace a value in this map, I get a warning that I need to see the reference to function template instantiation:
g_score.emplace(from_node->index, 0);
1>c:\users\accou\documents\pathfindingexamples\c++ library\pathfindinglib\pathfindinglib\pathfinding.cpp(44): note: see reference to function template instantiation 'std::pair<std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>,boo
l> std::_Hash<std::_Umap_traits<_Kty,float,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::emplace<IVector3&,int>(IVector3 &,int &&)' being compiled
1> with
1> [
1> _Ty=std::pair<const IVector3,float>,
1> _Kty=IVector3,
1> _Hasher=IVector3Hash,
1> _Keyeq=std::equal_to<IVector3>,
1> _Alloc=std::allocator<std::pair<const IVector3,float>>
1> ]
I've looked through the documentation for std::pair and std::unordered_map but I can't see what I'm doing wrong. The code compiles, but I don't want errors to occur if other compilers are used.
Thank you for any help :)
EDIT to include full warning text: https://pastebin.com/G1EdxKKe
I was confused by the long-winded error output, but the actual error was because I was trying to emplace(...) using an int rather than a float as the map required.
Changing to:
g_score.emplace(from_node->index, 0.0f);
solved the issue.
Related
I have two classes: Lattice and ModelGUI. I want to pass function from Lattice to GUI as a callback. I implemented Lattice as unique_ptr. Some code:
ModelGUI.h:
using CheckTypeClbk = std::function<Enums::AgentType(int)>;
ModelGUI(const Matrix* matrix_, CheckTypeClbk callback, float windowHeight_, float windowWidth_, float latticeWidth_);
main.cpp:
std::unique_ptr<ILattice> lattice(new Lattice(5, qMap));
ModelGUI gui(lattice->getLattice(), std::bind(&ILattice::checkAgentType, lattice, std::placeholders::_1),
800, 1200, 800);
With this implementation I got strange compilation errors about templates:
1>main.cpp
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(390): error C2664: 'std::tuple<std::unique_ptr<ILattice,std::default_delete<_Ty>>,std::_Ph<1>>::tuple(std::tuple<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::_Ph<1>> &&)': cannot convert argument 1 from 'std::unique_ptr<ILattice,std::default_delete<_Ty>>' to 'std::allocator_arg_t'
1> with
1> [
1> _Ty=ILattice
1> ]
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(389): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(1902): note: see reference to function template instantiation 'std::_Compressed_pair<Enums::AgentType (__cdecl ILattice::* )(int),std::tuple<std::unique_ptr<ILattice,std::default_delete<_Ty>>,std::_Ph<1>>,false>::_Compressed_pair<Enums::AgentType(__cdecl ILattice::* )(int),_Cv_TiD&,const std::_Ph<1>&>(std::_One_then_variadic_args_t,_Other1 &&,_Cv_TiD &,const std::_Ph<1> &)' being compiled
1> with
1> [
1> _Ty=ILattice,
1> _Cv_TiD=std::unique_ptr<ILattice,std::default_delete<ILattice>>,
1> _Other1=Enums::AgentType (__cdecl ILattice::* )(int)
1> ]
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(1903): note: see reference to function template instantiation 'std::_Compressed_pair<Enums::AgentType (__cdecl ILattice::* )(int),std::tuple<std::unique_ptr<ILattice,std::default_delete<_Ty>>,std::_Ph<1>>,false>::_Compressed_pair<Enums::AgentType(__cdecl ILattice::* )(int),_Cv_TiD&,const std::_Ph<1>&>(std::_One_then_variadic_args_t,_Other1 &&,_Cv_TiD &,const std::_Ph<1> &)' being compiled
1> with
1> [
1> _Ty=ILattice,
1> _Cv_TiD=std::unique_ptr<ILattice,std::default_delete<ILattice>>,
1> _Other1=Enums::AgentType (__cdecl ILattice::* )(int)
1> ]
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(1902): note: while compiling class template member function 'std::_Binder<std::_Unforced,Enums::AgentType (__cdecl ILattice::* )(int),std::unique_ptr<ILattice,std::default_delete<_Ty>> &,const std::_Ph<1> &>::_Binder(_Fx &&,std::unique_ptr<_Ty,std::default_delete<_Ty>> &,const std::_Ph<1> &)'
1> with
1> [
1> _Ty=ILattice,
1> _Fx=Enums::AgentType (__cdecl ILattice::* )(int)
1> ]
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(1929): note: see reference to function template instantiation 'std::_Binder<std::_Unforced,Enums::AgentType (__cdecl ILattice::* )(int),std::unique_ptr<ILattice,std::default_delete<_Ty>> &,const std::_Ph<1> &>::_Binder(_Fx &&,std::unique_ptr<_Ty,std::default_delete<_Ty>> &,const std::_Ph<1> &)' being compiled
1> with
1> [
1> _Ty=ILattice,
1> _Fx=Enums::AgentType (__cdecl ILattice::* )(int)
1> ]
1>d:\predator-prey\predator-prey\main.cpp(16): note: see reference to class template instantiation 'std::_Binder<std::_Unforced,Enums::AgentType (__cdecl ILattice::* )(int),std::unique_ptr<ILattice,std::default_delete<_Ty>> &,const std::_Ph<1> &>' being compiled
1> with
1> [
1> _Ty=ILattice
1> ]
But when I am using shared_ptr instead of unique_ptr, everything works fine. Is a good practice? I've heard to avoid shared_ptr as much as I can, unless they are totally necessary.
Do you need shared_ptr?
No. At least not for the given example.
If lattice and gui are defined in different scopes with different lifespans and being used all over the place, wowie-zowie, we can talk about shared_ptr.
Why?
Let's start with a really simple example that shows why unique_ptr is causing grief.
#include <functional>
#include <iostream>
struct test
{
test() = default;
test(const test &)
{
std::cout << "copied" << std::endl;
}
void func(int i)
{
std::cout << i << std::endl;
}
};
int main()
{
test t;
std::function<void(int)> f1 = std::bind(&test::func, t, std::placeholders::_1);
f1(1);
}
test doesn't do much other than tell us when the object is copied and prove that the function ran. Executing it we'll see that t is copied and produced the expected output from the function.
std::unique_ptr cannot be copied because that would pretty much ruin the whole unique part of the job description. We see that if we change main a little to use a unique_ptr and get a little closer to the question as posed.
int main()
{
std::unique_ptr<test> tp = std::make_unique<test>();
std::function<void(int)> f1 = std::bind(&test::func, tp, std::placeholders::_1);
}
As expected, this doesn't compile. We can make this compile by using a std::reference_wrapper
std::function<void(int)> f1 = std::bind(&test::func, std::reference_wrapper<std::unique_ptr<test>>(tp), std::placeholders::_1);
or provide a raw pointer to bind
std::function<void(int)> f1 = std::bind(&test::func, tp.get(), std::placeholders::_1); f1(1);
but this requires tp to have wider scope and be guaranteed to outlive f1. What this really comes down to is why use more than test t; in the first place? Do we really need a pointer here at all?
But let's go with it for now because we can at least make this look a lot prettier before we head off to greener pastures. Here is the same thing with a lambda expression
std::function<void(int)> f1 = [&tp](int i) { tp->func(i); };
Normally I'm not a champion of "Lambda is easier to read than bind," but this case is a pretty convincing argument.
Getting back to basics, it's not really all that different from
int main()
{
test t;
std::function<void(int)> f1 = [&t](int i) { t.func(i); };
f1(1);
}
and completely eliminates the pointer. No pointer, no shared_ptr.
If t can be fire and forget, the only user is the callback, let the lambda carry around a copy of t and let the original die.
std::function<void(int)> scopedemo()
{
test t;
return [t](int i) mutable { t.func(i); }; //
}
int main()
{
auto f1 = scopedemo();
f1(1);
}
Note the mutable. Lambda defaults to carrying around constants and can't be used to invoke non-const methods or be used as a non-const parameter.
You are passing std::unique_ptr by value and it's always a bad idea, because it doesn't have a copy constructor.
Why doesn't your code even compile? It seems that passing std::unique_ptr to a bound function is a long-living bug of visual studio.
I want to pass the standardized C++ binary functions to a template function, but somehow I didn't get it to work.
The following is my attempt to do it:
template<template <typename> typename Pred,typename T, typename Iterator>
void iota_stepa(Iterator begin, Iterator end, T startofSequence_, T threadStep)
{
int currMaxThreads = startofSequence_;
bool first = true;
generate(begin, end, Pred<T>(currMaxThreads, threadStep) );
}
and testing it with:
vector<int> tempVect_(10, 0);
iota_stepa<std::plus>(begin(tempVect_),end(tempVect_),1,thread::hardware_concurrency());
gives me unfortunately the errors:
Severity Code Description Project File Line Suppression State
Error C2440 '<function-style-cast>': cannot convert from 'initializer list' to 'std::plus<int>'
Error C2672 'generate': no matching overloaded function found FractalCarpet
Error C2780 'void std::generate(_FwdIt,_FwdIt,_Fn0)': expects 3 arguments - 2 provided FractalCarpet
The console output looks like the following:
1> c:\users\mtunca\documents\esd\sps\fractalcarpet\main.cpp(55): note: see reference to function template instantiation 'void iota_stepa<std::plus,int,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<float>>>>(Iterator,Iterator,T,T)' being compiled
1> with
1> [
1> Iterator=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<float>>>,
1> T=int
1> ]
1>c:\users\mtunca\documents\esd\sps\fractalcarpet\main.cpp(34): error C2672: 'generate': no matching overloaded function found
1>c:\users\mtunca\documents\esd\sps\fractalcarpet\main.cpp(34): error C2780: 'void std::generate(_FwdIt,_FwdIt,_Fn0)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: see declaration of 'std::generate'
Could someone help me, how to solve this problem?
std::generate needs a generator, something that can be called like gen().
You could create one with a lambda, perhaps like this:
template<template <typename> class Pred, typename T, typename Iterator>
void iota_stepa(Iterator begin, Iterator end, T startofSequence_, T threadStep)
{
bool first = true;
T current;
auto gen = [&]() -> T
{
if(first) {
current = startofSequence_;
first = false;
} else {
current = Pred<T>() ( current, threadStep );
}
return current;
};
generate(begin, end, gen );
}
Pred<T>(currMaxThreads, threadStep) );
Pred<T> is a type. You need to construct an actual callable object:
Pred<T>()(currMaxThreads, threadStep) );
This however cannot be the last argument to std::generate. The latter requires a callable object with no arguments, presumably holding a state (otherwise a call to std::fill woud suffice). It is unclear how an arbitrary binary function could be adapted to fill this role.
I didn't really know what to write in the title, but basically I have a single .cpp, with only standard library headers included and no "using" keywords. I made my own "generate(...)" function. After including the library, Visual Studio shows me an error (where the function is being called), basically saying that it doesn't know whether to choose std::generate(...) or generate(...) because they have matching argument lists.
Is this a bug or have I missed something? I might also add that I am using VS2015.
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
template<typename Iter, typename Function>
Function generate(Iter begin, Iter end, Function f)
{
while (begin != end)
{
*begin = f();
++begin;
}
return f;
}
class Random
{
public:
Random(int low, int high)
: mLow(low), mHigh(high)
{}
int operator()()
{
return mLow + rand() % (mHigh - mLow + 1);
}
private:
int mLow;
int mHigh;
};
class Print
{
void operator()(int t)
{
std::cout << t << " ";
}
};
int main()
{
srand(time(0));
std::vector<int> intVec;
intVec.resize(15);
Random r(2, 7);
generate(intVec.begin(), intVec.end(), r);
}
Error output:
1>------ Build started: Project: Functor, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(44): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): error C2668: 'generate': ambiguous call to overloaded function
1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(7): note: could be 'Function generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(Iter,Iter,Function)'
1> with
1> [
1> Function=Random,
1> Iter=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: or 'void std::generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(_FwdIt,_FwdIt,_Fn0)' [found using argument-dependent lookup]
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,
1> _Fn0=Random
1> ]
1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): note: while trying to match the argument list '(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, Random)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This happens on not just VC++ (VS 2015), but g++ 4.9+ as well. The issue here is the tricky Argument Dependent Lookup (Koenig Lookup).
It looks at the two iterators you're adding and it sees the "generate" function in std because the iterators also come from the std namespace (this is the point of Argument Dependent Lookup).
This problem actually bit me at one point: when I wrote my own tie implementation that did a few things extra to tie. I had to call mine tye because Koenig Lookup caused the considered overloads to be equal in their ranking and thus cause an error like this.
Either prefix generate with :: to start lookup from the global namespace (::generate( vec.begin(), vec.end(), ... );), or name it differently.
Edit:
This has been reported as a VS2012 C++ compiler bug on Microsoft Connect (link).
Nov. 11, 2014: Microsoft has responded saying the fix for this bug should show up in the next major release of Visual C++.
I've been struggling with a VS2012 compiler error message I don't understand, so I trimmed down the problem to what seems like the bare minimum.
I'm building the following main.cpp using VS2012:
#include <utility>
template <typename T>
struct A
{
T x;
A(A&& other) : x(std::move(other.x)) { }
A(T&& x) : x(std::move(x)) { }
};
template <typename T>
A<T> build(T&& x)
{
return A<T>(std::move(x));
}
int main(int argc, char* argv[])
{
auto f = []()
{
return build([](){}); //error here
};
return 0;
}
The salient point is that I'm trying to use a lambda as the template type T of the build function. The error message I get is:
1> main.cpp
1>C:\test\main.cpp(21): error C2664: 'A<T>::A(A<T> &&)' : cannot convert parameter 1 from 'A<T>' to 'A<T> &&'
1> with
1> [
1> T=void (__cdecl *)(void)
1> ]
1> and
1> [
1> T=main::<lambda_c3c618d445b3cb24eede9bf304860ad7>::()::<lambda_4240e93016e3e420ff8383c9350ae130>
1> ]
1> and
1> [
1> T=void (__cdecl *)(void)
1> ]
1> Reason: cannot convert from 'A<T>' to 'A<T>'
1> with
1> [
1> T=main::<lambda_c3c618d445b3cb24eede9bf304860ad7>::()::<lambda_4240e93016e3e420ff8383c9350ae130>
1> ]
1> and
1> [
1> T=void (__cdecl *)(void)
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I've done my research and looked up the page for the error message (link), but I still can't figure out what the problem is. Could you please explain this compiler error?
edit
Something is definitely weird here. If I change the code in main to look like this:
auto f = []()
{
int* n = new int(0);
auto g = [=](){ return *n; };
*n++;
return build<decltype(g)>(std::move(g));
};
I get an error message suggesting that T=int (__cdecl *)(void) in the call to build - which would mean that decltype(g) is giving me a function pointer? Huh? I'm capturing a pointer by value and then modifying it afterwards - shouldn't it have to create a functor - and one that has no cast to function pointer? Maybe I'm not understanding something.
See related: Lambda expressions : n3290 draft
Also, if this is a bug in the VS2012 compiler, can you think of a workaround?
I can confirm that using GCC (on linux), this code compiles just fine.
So I'd say that VisualStudio seems to be the source of the error.
I don't have Windows or Visual Studio to verify, nor do I have much experience with lambda functions in C++, but perhaps you need to include the (albeit empty) parameter list in the function? i.e. change line 21 to
return build([](){});
Both versions compile with GCC, but perhaps Visual Studio is a bit more picky.
The other question I might have is whether the lambda function you're defining at line 24 will work out since its return value involves the lambda function you're defining inside the function itself.
I do not know if that behavior comply with the standard but with VC++ 2019 that error happen only with the option /permissive-, then when the strict mode is on.
Nevertheless here is how to solve the problem, by just casting the lambda with a reference type:
template <typename FUNC>
void f(FUNC& o){}
int main()
{
f((std::function<void()>&)[](){});
// or also:
auto func = [](){};
f(func);
}
I'm getting a warning for the following code, which dissapears if I remove boost::blank from the variant:
namespace DB
{
struct Value{};
struct Container{};
}
typedef boost::variant <boost::blank, DB::Value, DB::Container> CommandData;
struct Command {
explicit Command(CommandData& _data): data(_data){
}
CommandData data;
};
int main()
{
CommandData commandData;
Command command(commandData);
return 0;
}
What's this issue?
Here's the warning:
1>: warning C4345: behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
1> c:\boost_1_49_0\boost\variant\variant.hpp(1224) : while compiling class template member function 'boost::variant<T0_,T1,T2>::variant(void)'
1> with
1> [
1> T0_=boost::blank,
1> T1=DB::Value,
1> T2=DB::Container
1> ]
1> c:\code.h(38) : see reference to class template instantiation 'boost::variant<T0_,T1,T2>' being compiled
1> with
1> [
1> T0_=boost::blank,
1> T1=DB::Value,
1> T2=DB::Container
1> ]
That warning is rather dumb. It warns that MSVC now does the right thing as opposed to some ancient version. You can turn it off with a pragma.
It's not because of the variant. Try to put int as a struct member for example instead of variant, and you'll get the same warning. The thing is that variant initializes with the first value by default, and boost::blank is a spectial type to optimize the variant behavior. See the variant documentation in Boost