I have a class
on a header file, which has its members defined inside a pimpl class. The idea is that I use this method (basically std::aligned_storage_t
and a pointer, but the size and alignment of the class have to be specified when declaring the object) to allocate the pimpl class on the stack. I want to make the code cross-compiler so guessing isn't an option, thus I defined 2 private
static constexpr
functions: impl_size
and impl_align
which are defined on the corresponding source file and basically return sizeof(pimpl)
and alignof(pimpl)
. The problemm is that I get the following error from MSVC (not tested on other compilers):
expression must have a constant value -- constexpr function function "Type::impl_size" (declared at line 70) is not defined
line 70 is where impl_size
is defined on the header.
MCVE:
#include <cstddef>
template <typename T1, std::size_t N1, std::size_t N2>
struct Pimpl;
class Type
{
private:
static constexpr std::size_t impl_size() noexcept; // line 70
static constexpr std::size_t impl_align() noexcept;
struct impl {};
Pimpl<impl, impl_size(), impl_align()> data; // error happens here
};
constexpr std::size_t Type::impl_size() noexcept
{
return sizeof(Type::impl);
}
constexpr std::size_t Type::impl_align() noexcept
{
return alignof(Type::impl);
}