Created
June 14, 2022 10:05
-
-
Save dyigitpolat/c3d8ad3768c4ccb21f8c6799a9136e5b to your computer and use it in GitHub Desktop.
enforce an argument to be known at compile time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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