मेरे पास है:कम से कम तर्कों पर एक भिन्न टेम्पलेट टेम्पलेट पैरामीटर विशेषज्ञता: कानूनी या नहीं? कोड
#include <cstdio>
template<template<typename...> class>
struct Foo
{
enum { n = 77 };
};
template<template<typename, typename...> class C>
struct Foo<C>
{
enum { n = 99 };
};
template<typename...> struct A { };
template<typename, typename...> struct B { };
int main(int, char**)
{
printf("%d\n", Foo<A>::n);
printf("%d\n", Foo<B>::n);
}
विचार है कि template<typename, typename...> class
template<typename...> class
के एक सबसेट है, तो यह उस पर विशेषज्ञ करने के लिए संभव हो सकता है। लेकिन यह बहुत गूढ़ है, तो शायद नहीं। आइए इसे आज़माएं।
जीसीसी 4.7 का कहना है:
$ g++ -std=c++11 test157.cpp
यह संकलित!
यह चल रहा है:
$ ./a.out
77
99
यह काम करता है!
बजना 3.1 का कहना है:
$ clang++ -std=c++11 test157.cpp
test157.cpp:10:8: error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
struct Foo<C>
^~~~
test157.cpp:9:10: error: too many template parameters in template template parameter redeclaration
template<template<typename, typename...> class C>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test157.cpp:3:10: note: previous template template parameter is here
template<template<typename...> class>
^~~~~~~~~~~~~~~~~~~~~
2 errors generated.
कौन सही है?
धन्यवाद। आकर्षक उदाहरण। इसके बारे में सोचते हुए, मेरा उदाहरण किसी भिन्न-विशिष्ट संख्या पैरामीटर (कहें, दो) के लिए एक भिन्न टेम्पलेट टेम्पलेट पैरामीटर को विशेषज्ञता देने के सामान्य व्यक्ति का एक विशिष्ट मामला था। यदि मैं 'टेम्पलेट <टाइपनाम, टाइपनाम> कक्षा' के लिए 'Foo' का विशेषज्ञता बनाने का प्रयास करता हूं, तो मुझे वही व्यवहार मिलता है: जीसीसी स्वीकार करता है, क्लैंग अस्वीकार करता है। मैं एक बग फाइल करूंगा (अगर यह नहीं किया गया है)। मानक से एक उद्धरण मुझे खुश कर देगा, अगर यह बहुत अधिक परेशानी नहीं है। – glaebhoerl