Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
engun opened this issue Dec 7, 2023 · 6 comments

Comments

@engun
Copy link

engun commented Dec 7, 2023

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 added a commit that referenced this issue Dec 7, 2023
@greg7mdp
Copy link
Owner

greg7mdp commented Dec 7, 2023

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
Copy link
Author

engun commented Dec 9, 2023

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
Copy link
Owner

greg7mdp commented Dec 9, 2023

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

greg7mdp added a commit that referenced this issue Dec 9, 2023
@engun
Copy link
Author

engun commented Dec 9, 2023

  • Compiler: MSVC
  • System: win11 22h2

@greg7mdp
Copy link
Owner

greg7mdp commented Dec 9, 2023

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
Copy link
Author

engun commented Dec 10, 2023

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.

@engun engun closed this as completed Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants