greg7mdp / parallel-hashmap

A family of header-only, very fast and memory-friendly hashmap and btree containers.
https://greg7mdp.github.io/parallel-hashmap/
Apache License 2.0
2.53k stars 239 forks source link

In template: constexpr variable 'kFirst' must be initialized by a constant expression #224

Closed engun closed 10 months ago

engun commented 10 months ago

This is a great open-source project. I encountered a little problem while using it.

The compilation tool is CLion, and I'm using C++17.

#include <shared_mutex>
#include <vector>
#include <iostream>

#include "parallel_hashmap/phmap.h"

template <class K, class V>
using HashT = phmap::parallel_flat_hash_map<K, V, phmap::priv::hash_default_hash<K>,
        phmap::priv::hash_default_eq<K>,
        std::allocator<std::pair<const K, V>>, 4, std::shared_mutex>;

int main() {
    HashT<uint64_t, uint32_t> hash;
    hash[1] = 1;
    std::cout << hash.at(1) << std::endl;
    return 0;
}

The compiler is showing an annoying error message:

"In template: constexpr variable 'kFirst' must be initialized by a constant expression."

greg7mdp commented 10 months ago

Hi @engun , thanks for reporting this. I'm not using CLion, but I just made a change that I think may fix this. Can you try the latest version of the repo?

engun commented 10 months ago

I just tried it. It's still the same. maybe more detailed information would help.

begin----------------------------------------------------------------------------------------------------------

In template: constexpr variable 'kFirst' must be initialized by a constant expression

error occurred here 
in instantiation of static data member 'phmap::priv::memory_internal::OffsetOf<phmap::priv::memory_internal::IsLayoutCompatible<unsigned long long, unsigned int>::Pair>::kFirst' requested here 
in instantiation of static data member 'phmap::priv::memory_internal::IsLayoutCompatible<unsigned long long, unsigned int>::value' requested here 
in instantiation of function template specialization 'phmap::priv::map_slot_policy<unsigned long long, unsigned int>::transfer<std::allocator<std::pair<const unsigned long long, unsigned int>>>' requested here 
in instantiation of function template specialization 'phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>::transfer<std::allocator<std::pair<const unsigned long long, unsigned int>>>' requested here 
in instantiation of function template specialization 'phmap::priv::hash_policy_traits<phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>>::transfer_impl<std::allocator<std::pair<const unsigned long long, unsigned int>>, ph... (skipping 4 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all) 
in instantiation of function template specialization 'phmap::priv::parallel_hash_set<4, phmap::priv::raw_hash_set, std::shared_mutex, phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>, phmap::Hash<uint64_t>, phmap::EqualT... 
in instantiation of function template specialization 'phmap::priv::parallel_hash_map<4, phmap::priv::raw_hash_set, std::shared_mutex, phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>, phmap::Hash<uint64_t>, phmap::EqualT... 
in instantiation of function template specialization 'phmap::priv::parallel_hash_map<4, phmap::priv::raw_hash_set, std::shared_mutex, phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>, phmap::Hash<uint64_t>, phmap::EqualT... 
in instantiation of function template specialization 'phmap::priv::parallel_hash_map<4, phmap::priv::raw_hash_set, std::shared_mutex, phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>, phmap::Hash<uint64_t>, phmap::EqualT... 
in instantiation of function template specialization 'phmap::priv::parallel_hash_map<4, phmap::priv::raw_hash_set, std::shared_mutex, phmap::priv::FlatHashMapPolicy<unsigned long long, unsigned int>, phmap::Hash<uint64_t>, phmap::EqualT...

:4387:39: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression

Declaration location: phmap.h

namespace phmap::priv  
public: 
template<K = key_type, P = Policy, K *  = nullptr> 
MappedReference<P> parallel_hash_map::operator[](key_arg<K> &&key)

end----------------------------------------------------------------------------------------------------------

greg7mdp commented 10 months ago

What compiler are you using within CLion, on what operating system?

engun commented 10 months ago
greg7mdp commented 10 months ago

Hum, weird, I cannot reproduce the issue with Visual Studio 2022. Do you have a custom version of the macro offsetof in your code? Either that or you use an older compiler which provides a non constexpr version of offsetof.

engun commented 10 months ago

Yes, VS doesn't show this error and, in fact, doesn't affect CLION compilation, but my OCD tells me to raise an issue. after a brief study, this error is from the Clang prompt.