Template inside template Question VS 2005?

The code is below (it compiles fine with clang and gcc). The question is, does this code violate the C ++ 03 standard, or is it a VS 2005 error? And if it's a mistake, any work around?

Update: I found a workaround using a forward declaration:

//forward declaration template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted> struct FilteredConstructor; //implementation template<typename T> class FilteredConstructor<T, true> {/*code here*/}; 

But the question of whether this code is valid or invalid according to the standard is still here

 namespace { struct CoreTypesFilter { template<typename T> struct Acceptor { static const bool IsAccepted = false; }; }; } template<class Filter> class QVariantConstructor { template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted> struct FilteredConstructor { FilteredConstructor(const QVariantConstructor &tc) {} }; template<typename T> struct FilteredConstructor<T, /* IsAcceptedType = */ false> { FilteredConstructor(const QVariantConstructor &tc) {} }; public: template<typename T> void delegate(const T*) { FilteredConstructor<T> tmp(*this); } }; //comment or uncomment them to build on VS or linux #define _TCHAR char #define _tmain main int _tmain(int argc, _TCHAR* argv[]) { QVariantConstructor<CoreTypesFilter> vc; vc.delegate("test");//this line trigger compile error return 0; } 

Compilation errors of the VS 2005 compiler:

  error C2976: 'QVariantConstructor :: FilteredConstructor': too few template arguments

 1> with
 1> [
 1> Filter = `anonymous-namespace ':: CoreTypesFilter
 1>]
 1> see declaration of 'QVariantConstructor :: FilteredConstructor'
 1> with
 1> [
 1> Filter = `anonymous-namespace ':: CoreTypesFilter
 1>]
 1> see reference to function template instantiation 'void QVariantConstructor :: delegate (const T *)' being compiled
 1> with
 1> [
 1> Filter = `anonymous-namespace ':: CoreTypesFilter,
 1> T = char
 1>]
 1> error C2514: 'QVariantConstructor :: FilteredConstructor': class has no constructors
 1> with
 1> [
 1> Filter = `anonymous-namespace ':: CoreTypesFilter
 1>]
 1> see declaration of 'QVariantConstructor :: FilteredConstructor'
 1> with
 1> [
 1> Filter = `anonymous-namespace ':: CoreTypesFilter
 1>]
+4
source share
1 answer

I quickly read the relevant section of the standard, and the problem seems to boil down to how you read section 9.1 of section 14.1 of ISO / IEC 14882: 2003

The default template-argument is template-argument (14.3), specified after = in the template-parameter . By default, template-argument can be specified for any type of template-parameter (type, non-type, template). By default, template-argument can be specified in a class template declaration or class template definition. By default, template-argument not specified in the function template declaration or function template definition or in the template-parameter-list definition of a class template member . By default, template-argument not specified in the friend template declaration.

Strictly speaking, this means that your code is illegal. However, most implementations seem to interpret this as meaning definitions of an already declared template member.

Health hazard warning, this is based on the first reading. My answer may be incorrect or incomplete, please correct me.

0
source

Source: https://habr.com/ru/post/1499810/


All Articles