How to specialize a template function for the case that the value of one of its argument is known/unknown during compile time (before actually compile and run the program)?
I can't figure out how yet.
idea 1:
#include <type_traits>
#include <iostream>
int main(void){
int a; //value of a is not known at compile time
bool b = (a == a); //value of b is known at compile time.
std::is_assignable< constexpr bool, bool >::value
}
//g++ magic.cpp -std=c++14
//error: wrong number of template arguments (1, should be 2)
// std::is_assignable< constexpr bool, bool >::value
Idea 2:
#include <type_traits>
#include <iostream>
int main(void){
const int a=1;
int b = (a == a);
std::cout << __builtin_constant_p (a) << std::endl;
std::cout << __builtin_constant_p (b) << std::endl;
}
//prints 0 and 0.
constexpr
argument. I am thinking about specialization by trait instead of redefiningconstexpr
.... or if there any way that works. – Acidosisconstexpr
parameter specification, which, as the answer says, doesn’t exist. I don’t think this can be emulated via a trait hough I might be mistaken. Best you’ll be able to achieve is check for whether the argument is a prvalue. But there are non-compile-time values that still satisfy this value category. – Traveled