martinus / robin-hood-hashing

Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
https://gitter.im/martinus/robin-hood-hashing
MIT License
1.52k stars 146 forks source link

"Chosen constructor is explicit in copy-initialization" #97

Closed Izhido closed 3 years ago

Izhido commented 3 years ago

The following code, which is a simplification of some code that is already in production in a released application, triggers the error above:

#include <iostream>
#include <vector>
#include <unordered_map>
#include "robin_hood.h"

struct Texture
{
    int width;
    int height;
    void* data;
};

struct PerImage
{
    std::unordered_map<void*, Texture*> textureIndex;
};

struct Scene
{
    std::vector<PerImage> perImage;
    robin_hood::unordered_map<void*, Texture*> texturesPerKey;
};

struct AppState
{
    Scene scene;
};

int main(int argc, const char * argv[])
{
    AppState appState {};
    std::cout << "Success!\n";
    return 0;
}

The explicit error message is as follows:

/Users/heribertodelgado/Projects/testhashmap/testhashmap/main.cpp:31:24: error: chosen constructor is explicit in copy-initialization
    AppState appState {};
                       ^
In file included from /Users/heribertodelgado/Projects/testhashmap/testhashmap/main.cpp:4:
/Users/heribertodelgado/Projects/testhashmap/testhashmap/robin_hood.h:1764:14: note: explicit constructor declared here
    explicit Table(
             ^
/Users/heribertodelgado/Projects/testhashmap/testhashmap/main.cpp:21:48: note: in implicit initialization of field 'texturesPerKey' with omitted initializer
    robin_hood::unordered_map<void*, Texture*> texturesPerKey;
                                               ^
/Users/heribertodelgado/Projects/testhashmap/testhashmap/main.cpp:26:11: note: in implicit initialization of field 'scene' with omitted initializer
    Scene scene;
          ^
1 error generated.

Just to be clear, if you replace robin_hood::unordered_map with std::unordered_map the error goes away.

Is there a workaround that would allow me to avoid this compilation error?

martinus commented 3 years ago

Hi, thanks for the report! as a workaround, you can remove explicit from the line explicit Table(

martinus commented 3 years ago

If you try the latest version, this should work now:

https://raw.githubusercontent.com/martinus/robin-hood-hashing/master/src/include/robin_hood.h