Skip to content

Instantly share code, notes, and snippets.

@dyigitpolat
Created June 14, 2022 10:05
Show Gist options
  • Save dyigitpolat/c3d8ad3768c4ccb21f8c6799a9136e5b to your computer and use it in GitHub Desktop.
Save dyigitpolat/c3d8ad3768c4ccb21f8c6799a9136e5b to your computer and use it in GitHub Desktop.
enforce an argument to be known at compile time
#include <iostream>
template <typename T>
struct constexpr_arg
{
T value;
consteval constexpr_arg(T obj) : value{obj} {}
operator T() { return value; }
};
int mul(constexpr_arg<int> a, int b)
{
return a*b;
}
int main()
{
int b{3};
std::cout << mul(5, b) << '\n'; //OK
std::cout << mul(b, b) << '\n'; //error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment