Why this constexpr does not create an error?

Viewed 58

As you know, memcpy() is not a constexpr. Any idea why this code compiles without error?

template<typename T>
constexpr void mymemcopy(T* a, void* b) {
    memcpy(a, b, sizeof(T));
}

I tested in both clang 12.0.0 and gcc 11.1 with -pedantic flag. It compiles in MSCV 19.28 too. I have rarely seen a similar bug for all compilers...

A full working sample is here:

template<typename T>
constexpr void mymemcopy(T* a, void* b) {
    memcpy(a, b, sizeof(T));
}

int main()
{
    double a, b = 10.0;

    mymemcopy(&a, &b);

    std::cout << a;
}
0 Answers
Related