निम्नलिखित कोड पर विचार करें:सी ++ 11 constexpr समारोह पास पैरामीटर
static constexpr int make_const(const int i){
return i;
}
void t1(const int i)
{
constexpr int ii = make_const(i); // error occurs here (i is not a constant expression)
std::cout<<ii;
}
int main()
{
t1(12);
}
मैं क्यों make_const कॉल पर एक त्रुटि है?
अद्यतन
लेकिन यह एक काम करता है:
constexpr int t1(const int i)
{
return make_const(i);
}
बहरहाल, यह नहीं:
template<int i>
constexpr bool do_something(){
return i;
}
constexpr int t1(const int i)
{
return do_something<make_const(i)>(); // error occurs here (i is not a constant expression)
}
ठीक है क्योंकि सामान्य मामले में, 'i'' void t1 (const int) में 'constexpr' नहीं है। – stefan
मैं इसे कैसे बना सकता हूं, फिर? – tower120
इसे एक टेम्पलेट तर्क बनाना आपका एकमात्र विकल्प है – stefan