निम्नलिखित कोड पर विचार करें:यदि संकलन समय पर/else?
#include <iostream>
#include <type_traits>
template<typename T> class MyClass
{
public:
MyClass() : myVar{0} {;}
void testIf() {
if (isconst) {
myVar;
} else {
myVar = 3;
}
}
void testTernary() {
(isconst) ? (myVar) : (myVar = 3);
}
protected:
static const bool isconst = std::is_const<T>::value;
T myVar;
};
int main()
{
MyClass<double> x;
MyClass<const double> y;
x.testIf();
x.testTernary();
y.testIf(); // <- ERROR
y.testTernary(); // <- ERROR
return 0;
}
एक्स (गैर स्थिरांक) के लिए वहाँ कोई समस्या नहीं है। लेकिन वाई (कॉन्स डेटा प्रकार) एक त्रुटि का कारण बनता है भले ही स्थिति/समय में संकलन समय पर ज्ञात हो।
क्या संकलन समय पर झूठी स्थिति को संकलित करने की कोई संभावना नहीं है?
क्या आप चाहते हैं एक 'स्थिर if' है, और यह हिस्सा नहीं है सी ++ के अभी तक (http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Static-If-I-Had-a- हथौड़ा) – arnoo