मैं एक टेम्पलेटेड संरचना के अंदर कुछ SFINAE का उपयोग करने की कोशिश कर रहा हूं। मैं निम्नलिखित करने के लिए मेरी समस्या कम है और यह काम कर सकता है: इस कोड के साथअपूर्ण प्रकार का अमान्य उपयोग (SFINAE)
template<bool mybool>
struct test {
void myfunc();
};
template<bool mybool>
void test<mybool>::myfunc() {
std::cout << "test true" << std::endl;
}
template<>
void test<false>::myfunc() {
std::cout << "test false" << std::endl;
}
int main(int argc, char ** argv) {
test<true> foo;
test<false> bar;
foo.myfunc();
bar.myfunc();
}
, मैं परिणाम प्राप्त:
test true
test false
हालांकि, अगर मैं एक से अधिक के साथ कि मेरी struct test
पर विचार करना चाहते टेम्पलेट पैरामीटर, मैं इस तरह से ऊपर अनुकूल की कोशिश की:
template<int myint, bool mybool>
struct test {
void myfunc();
};
template<int myint, bool mybool>
void test<myint,mybool>::myfunc() {
std::cout << "test true" << std::endl;
}
template<int myint>
void test<myint,false>::myfunc() {
//error: invalid use of incomplete type 'struct test<myint, false>'
std::cout << "test false" << std::endl;
}
int main(int argc, char ** argv) {
test<1,true> foo;
test<1,false> bar;
foo.myfunc();
bar.myfunc();
}
मैं अधूरा प्रकार 'struct परीक्षण' का गलत उपयोग हो रही है।
क्या मैं गलत दिशा में जा रहा हूं? क्या मैं ऐसा करना चाहता हूं जो मैं करना चाहता हूं? आपकी मदद के लिए धन्यवाद!
आपका मतलब है 'foo.myfunc (मैं उसका जवाब टिप्पणी नहीं कर सकता, इसलिए मैं अपने खुद के पोस्ट)()' जब आप 'लिखा foo.test()'? – bitmask
आपने दूसरे उदाहरण में 'myfunc' गलत भी लिखा है। यह 'my_func' होना चाहिए। कृपया उन्हें पोस्ट करने से पहले अपने उदाहरणों को आजमाएं। – pmr