हां। संभव डुप्लिकेट त्रुटि त्रुटि के रूप में आकार प्रिंट करता है, जिसका अर्थ है कि संकलन सफल नहीं होगा।
हालांकि, मेरा समाधान आकार को चेतावनी संदेश चेतावनी देता है, जिसका अर्थ है, यह आकार प्रिंट करेगा, और संकलन जारी रहेगा।
template<int N>
struct print_size_as_warning
{
char operator()() { return N + 256; } //deliberately causing overflow
};
int main() {
print_size_as_warning<sizeof(int)>()();
return 0;
}
चेतावनी संदेश:
prog.cpp: In member function ‘char print_size_as_warning<N>::operator()() [with int N = 4]’:
prog.cpp:8: instantiated from here
prog.cpp:4: warning: overflow in implicit constant conversion
डेमो: http://www.ideone.com/m9eg3
नोट: चेतावनी संदेश में एन के मूल्य sizeof का मूल्य (int)
है
उपरोक्त कोड एक में सुधार हुआ है, और मेरा पहला प्रयास यह था:
template<int N>
struct _{ operator char() { return N+ 256; } }; //always overflow
int main() {
char(_<sizeof(int)>());
return 0;
}
चेतावनी संदेश:
prog.cpp: In member function ‘_<N>::operator char() [with int N = 4]’:
prog.cpp:5: instantiated from here
prog.cpp:2: warning: overflow in implicit constant conversion
डेमो: http://www.ideone.com/mhXjU
विचार मेरे पिछले जवाब से इस सवाल का लिया जाता है:
क्या आप यह देख रहे हैं? http://stackoverflow.com/questions/2008398/is-it-possible-to-print-out-the-size-of-ac-class-at-compile-time – birryree
कई कंपाइलर्स में '# प्रगमा 'या अन्य निर्देश जो संकलन के दौरान मूल्यों को आउटपुट करने के लिए उपयोग किया जा सकता है। आपको अपने कंपाइलर के दस्तावेज की जांच करनी होगी। दुर्भाग्यवश मुझे नहीं पता कि यह उत्पादन उत्पन्न करने से पहले अभिव्यक्ति का मूल्यांकन करने के लिए कंपाइलर कैसे प्राप्त करें। –
नहीं, यह अलग सवाल है। –