I'm trying to pass a templated lambda to a function and then call it with a template parameter, to enable specialization of that function for custom types.
But when I try to call the lambda I get this error: error: invalid operands to binary expression
Here's a godbolt link for anyone who wants to play around with this: https://gcc.godbolt.org/z/qYPcea
#include <cstdint>
#include <string>
#include <cstring>
enum class Alignment : uint8_t {
one,
two,
four,
eight
};
template <Alignment alignment, typename T>
static void align(T& pointer)
{
intptr_t& value = reinterpret_cast<intptr_t&>(pointer);
value += (-value) & ((uint64_t)alignment - 1);
}
template<typename Lambda, typename T>
static void specialization(Lambda&& lambda, const T& t)
{
lambda<Alignment::eight>(t.data(), t.size());
}
int main()
{
uint8_t buffer[1024];
void *writeTo = buffer;
auto lambda = [&] <Alignment alignment> (const void *input, uint32_t inputSize) -> void
{
align<alignment>(writeTo);
writeTo = memcpy(writeTo, buffer, inputSize);
};
std::string input("helloworld");
specialization(lambda, input);
return 0;
}