473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template class instantiate without template parameter, automatictype deduction

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
Oct 25 '07 #1
0 1241

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1952
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 | KEY_CREATE_SUB_KEY) & (~SYNCHRONIZE)) #define MS_NBF "MNBF" #define PSN_FIRST (0U-200U)
4
2575
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code, and the error I get: -------------- namespace LIST { template <typename T> struct Link; template <typename T> class List;
8
3143
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int, 1> and A<int, 2> are different types. Now, if the A template
4
2556
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.
2
2368
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;
7
1526
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_;
3
1577
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) { }
3
3569
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
2
6626
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
8266
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.