Strangeness Pattern Template

Possible duplicate:
Is it possible to explicitly specify constructor template parameters?

after my previous question (I found this situation in edition 2)

I posted a simple code:

#include <iostream> struct Printer { Printer() { std::cout << "secret code" << std::endl; } }; template <class A> struct Class { template <class B, class C> Class(B arg) { C c; /* the 'secret code' should come from here */ std::cout << arg << std::endl; } Class(double arg) { std::cout << "double" << std::endl; } Class(float arg) { std::cout << "float" << std::endl; } /* this forbids the use of printer in the first parameter */ Class(Printer printer) { throw std::exception(); /* here be dragons */ } }; int main() { Class<int> c(1.0f); Class<int>* ptr = new Class<int>((double)2.0f); return 0; } // Can anyone print 'secret code' while creating an object of type 'Class' ? 

In detail. For the template constructor, can you specify a template argument that is not part of the constructor arguments when instantiating the object?

I think this deserves a separate question.

+1
source share
2 answers

No, It is Immpossible.

There is no syntax with which you can provide explicit template parameters to a constructor template. You can only provide explicit template parameters for the class template as a whole.

The following text from [temp.arg.explicit] (2003 wording, 14.8.1 / 5) is described in this scenario. Although the sentence is non-normative, it serves to explain to us that as an inherent limitation of grammar this is not possible:

Note: because the explicit template argument list follows the function template name and because the member function templates and constructor member function templates are called without using the name function , there is no way to provide an explicit list of template arguments for these function templates .

This, in part, is due to the fact that you never explicitly call the constructor yourself. When you write, say, A() , you do not call the constructor as a function, even if it looks like you are ("templates of conversion member functions and member members of the constructor are called without using the function name").

+2
source

I think he wants to know how to instantiate this class using C as SomeType:

 template<typename A> class foo { template<typename B, typename C> foo(B b) { C c; } }; 

I do not know if this is possible.

0
source

All Articles