为什么Google sparsehash开源库有两个实现:密集哈希表和稀疏哈希表?
所有测试用例都以某种方式包含<gtest/gtest.h>并且<google/dense_hash_map>无法为我构建.通常后者是间接包含但我可以重现这样的问题:
#include <gtest/gtest.h>
#include <google/dense_hash_map>
TEST(SparsehashTest, justPass) {
ASSERT_TRUE(true);
};
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)
问题:
In file included from /usr/include/c++/5/tr1/functional:39:0,
from /usr/local/include/sparsehash/dense_hash_map:106,
from /usr/local/include/google/dense_hash_map:34,
from /home/me/xxx/test/SparsehashTest.cpp:6:
/usr/include/c++/5/tr1/tuple:130:11: error: redefinition of ‘class std::tuple< <template-parameter-1-1> >’
class tuple : public _Tuple_impl<0, _Elements...>
^
In file included from /home/me/xxx/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:697:0,
from /home/me/xxx/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:40,
from /home/me/xxx/third_party/googletest/googletest/include/gtest/gtest.h:58,
from /home/me/xxx/test/SparsehashTest.cpp:5:
/usr/include/c++/5/tuple:463:11: error: previous definition of ‘class std::tuple< <template-parameter-1-1> >’
class tuple : public _Tuple_impl<0, _Elements...>
Run Code Online (Sandbox Code Playgroud)
所以sparsehash包括/usr/include/c++/5/tr1/tuple …
考虑这个简单的程序:
#include <string>
#include <sparsehash/dense_hash_map>
int main()
{
google::dense_hash_map<std::string, int> map;
map["foo"] = 0;
}
Run Code Online (Sandbox Code Playgroud)
使用GCC 8.2和-Wclass-memaccess(或-Wall)进行编译会产生警告:
sparsehash/internal/libc_allocator_with_realloc.h:68:40: warning:
‘void* realloc(void*, size_t)’ moving an object of non-trivially copyable type
‘struct std::pair<const std::__cxx11::basic_string<char>, int>’;
use ‘new’ and ‘delete’ instead [-Wclass-memaccess]
return static_cast<pointer>(realloc(p, n * sizeof(value_type)));
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
问题是:
我在这里提出了一个问题:https://github.com/sparsehash/sparsehash/issues/149
sparsehash ×3
c++ ×2
compilation ×1
g++ ×1
gcc8 ×1
googletest ×1
hash ×1
hashtable ×1
realloc ×1