मुझे संकलित त्रुटि मिली "त्रुटि: 'टेम्पलेट' कीवर्ड का उपयोग 'foo' को एक आश्रित टेम्पलेट नाम के रूप में करने के लिए करें" जब मैं टिप्पणी पंक्ति पर निम्न कोड संकलित करता हूं। (test4)क्या क्लास टेम्पलेट कक्षा टेम्पलेट गैर-निर्भर सदस्य चर में आवश्यक है?
कोड के सभी अन्य हिस्सों को सफलतापूर्वक संकलित किया गया है।
#include <tuple>
struct my {
template <typename T>
void foo() {}
};
void test1() {
my m;
auto tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>();
}
template <typename T>
struct test2 {
void method() {
my m;
auto tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>();
}
};
template <typename T>
struct test3 {
void method() {
m.foo<int>();
}
my m;
};
template <typename T>
struct test4 {
void method() {
auto tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>(); // error: use 'template' keyword to treat 'foo' as a dependent template name
t0.template foo<int>(); // OK
}
my m;
};
template <typename T>
struct test5 {
void method() {
std::tuple<my> tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>();
}
my m;
};
template <typename T>
struct test6 {
void method() {
auto tr = std::forward_as_tuple(m);
my& t0 = std::get<0>(tr);
t0.foo<int>();
}
my m;
};
int main() {
test1();
test2<int>().method();
test3<int>().method();
test4<int>().method();
test5<int>().method();
test6<int>().method();
}
test4
एक वर्ग टेम्पलेट है लेकिन मीटर गैर निर्भर प्रकार है।
मैंने जीसीसी और क्लैंग को संकलित करने की कोशिश की। जीसीसी 7.1.0 त्रुटियों की रिपोर्ट नहीं करते हैं, लेकिन क्लैंग 4.0 और बाद में संकलन त्रुटि की रिपोर्ट करते हैं।
त्रुटि
https://wandbox.org/permlink/HTSBJMD2kXwfWObl (बजना 4.0) https://wandbox.org/permlink/BcUT8gtaFxwC41c5 (बजना HEAD)
कोई त्रुटि
https://wandbox.org/permlink/GjIvZa3i5HB8uh6w (जीसीसी 7.1.0)
कौन सा सही व्यवहार है?
@StoryTeller, टिप्पणी के लिए धन्यवाद। Test3 और test5 को टेम्पलेट कीवर्ड की आवश्यकता क्यों नहीं है? –
आप जानते हैं, मैंने जो कहा वह खरोंच। अब मैं देखता हूं कि मैंने अन्य परीक्षणों को पूरी तरह नजरअंदाज कर दिया। महान सवाल – StoryTeller
त्रुटि नहीं। परीक्षण 5 में, जब आप 'std :: forward_as_tuple (m)' करते हैं, तो आपको 'std :: tuple' प्राप्त होता है, न कि 'std :: tuple '। यह वैसे भी परिणाम नहीं बदलता है। –
skypjack