Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
template <tpyename T>
void func(T a);
func(0.f);
This will cause func<floatto be instantiated. The user does not have
to explicitly call func<float>(0.f );
However this line of thinking is broken when it comes to a template
class constructor function, e.g.
class A{
int x;
};
template <typename T>
class C{
C(const T & t) : t(t) {}
T t;
};
template <>
class C<A{
C(const A & t) : t(t) {}
A t;
};
int main(){
A a;
C c(a);
}
The above code can't be successfully compiled. One has to name the
type returned from the constructor call to pick up the object. But
what really distinguishes from normal function call is that even C(a)
fails, compiler comlains missing template argument. The problem is
sometimes you want automatic (auto) type deduction that a compiler can
provide but you can't get it for constructor call.
What's the current best practice to approach such kind of problem,
i.e. automatic type deduction?
Fei 3 3561
Fei Liu wrote:
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
I apologize for the double posting...Wierd seamonkey email/newsreader
client issue.
Fei Liu wrote:
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
template <tpyename T>
void func(T a);
func(0.f);
This will cause func<floatto be instantiated. The user does not have
to explicitly call func<float>(0.f );
However this line of thinking is broken when it comes to a template
class constructor function, e.g.
class A{
int x;
};
template <typename T>
class C{
C(const T & t) : t(t) {}
T t;
};
template <>
class C<A{
C(const A & t) : t(t) {}
A t;
};
int main(){
A a;
C c(a);
}
The above code can't be successfully compiled. One has to name the
type returned from the constructor call to pick up the object. But
what really distinguishes from normal function call is that even C(a)
fails, compiler comlains missing template argument.
Correct. The syntax involves the _type_ name, not a function name.
The problem is
sometimes you want automatic (auto) type deduction that a compiler can
provide but you can't get it for constructor call.
That's not true at all. In your example it's not the constructor that
is a template for which the argument needs to be deduced, it's the type
template that needs to be instantiated. When constructors are concerned,
this is the correct (and working) example:
struct C {
template<class TC(const T& t) {}
};
struct A {};
int main() {
A a;
C c(a); // compiles just fine
}
What's the current best practice to approach such kind of problem,
i.e. automatic type deduction?
Wrap it in a function template.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote:
Fei Liu wrote:
>Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g.
template <tpyename T> void func(T a);
func(0.f);
This will cause func<floatto be instantiated. The user does not have to explicitly call func<float>(0.f );
However this line of thinking is broken when it comes to a template class constructor function, e.g.
class A{ int x; };
template <typename T> class C{ C(const T & t) : t(t) {} T t; };
template <> class C<A{ C(const A & t) : t(t) {} A t; };
int main(){
A a; C c(a); }
The above code can't be successfully compiled. One has to name the type returned from the constructor call to pick up the object. But what really distinguishes from normal function call is that even C(a) fails, compiler comlains missing template argument.
Correct. The syntax involves the _type_ name, not a function name.
>The problem is sometimes you want automatic (auto) type deduction that a compiler can provide but you can't get it for constructor call.
That's not true at all. In your example it's not the constructor that
is a template for which the argument needs to be deduced, it's the type
template that needs to be instantiated. When constructors are concerned,
this is the correct (and working) example:
struct C {
template<class TC(const T& t) {}
};
Well you see this is not the complete code I presented intially. You
threw away class member t with your convenient modification. If it needs
to be:
template <typenaem U>
struct C{
template<class TC(const T& t) : t(t) {}
U t;
};
We are back to step 1, once you have a class member whose type depends
on class template parameter.
>
struct A {};
int main() {
A a;
C c(a); // compiles just fine
}
>What's the current best practice to approach such kind of problem, i.e. automatic type deduction?
Wrap it in a function template.
Indeed I played with it for a bit, but it does not seem to be that
great, for example I could conjure a function like following to simulate
a constructor:
template <typename T, template <typename class RT>
RT<Tcreate(cons t T& t){
return RT<T>(t);
}
It quickly becomes obvious that RT<Tneeds to be given to instantiate
it and one does not gain anything.
Care to supply an example to illustrate your point?
>
V
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: bartek |
last post by:
Please consider the following scenario below (sketch of).
There are two templates defined: A and B, both with mutual conversion
operators defined.
Also, there's a free function template 'do_something' with the following
signature:
template <class T>
inline void do_something(B<T> const& arg);
Now, given an object of type, say, A<int>...
|
by: Thomas Heller |
last post by:
I need to convert C preprocessor definitions into python code. The
definitions are dumped out of gccxml (see http://www.gccxml.org) ,
running over the windows header files (for example).
This creates output like this (some excerpts):
#define DATABITS_8 ((WORD)0x0008)
#define KEY_WRITE ((STANDARD_RIGHTS_WRITE | KEY_SET_VALUE |...
|
by: Neelesh |
last post by:
Hi all,
I had some confusion about deduction of non-type template parameters
for function templates :
template <class T, int i> void foo (T, int p = i) ;
void bar()
{
foo<int, 10>(40,40); // OK, T = int, i = 10
foo(40,40); // ERROR, T = int but i cannot be deduced.
|
by: coolpint |
last post by:
Can anyone kindly provide an explanation as to why the compiler does
not "see" the function template in the contrieved code below?
I think the argument deduction fails but can't figure out exactly what
the problem is. Is it related to the fact that a nested class is
involved?
#include <iostream>
using std::cout;
|
by: Dilip |
last post by:
This is just an academic question but is there any particular reason
why this does not work?
template<typename T>
class Foo
{
public:
Foo(T x) : myvar_(x) { }
private:
T myvar_;
| |
by: =?gb2312?B?wfXquw==?= |
last post by:
Hi, folks,
I'm running into a question as below:
template<typename T>
class A {
private:
T _a;
public:
A(T t): _a(t) {
}
|
by: Fei Liu |
last post by:
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
template <tpyename T>
void func(T a);
func(0.f);
This will cause func<floatto be instantiated. The user does not have
|
by: David Sanders |
last post by:
Hi,
I have a class depending on a template parameter:
template<int n>
class MyClass {
};
I want the template parameter to be chosen according to a variable,
e.g.
|
by: Clyde |
last post by:
Hi,
what i'm trying to do is:
/////////////// Code Start
template <class TType, int* p = 0>
class Template
{
public:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |