473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About the instantiation of a template class

Hi, folks,
I'm running into a question as below:

template<typena me T>
class A {
private:
T _a;
public:
A(T t): _a(t) {
}
};

template<typena me T>
void operator+(A<Tlh s, A<Trhs) {
}

int main() {
A<intobj(5);
int t = 5;
obj + t;
}

When I try to compile the code above under MSVC8, I got the following
errors:

1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'

2. error C2676: binary '+' : 'A<T>' does not define this operator or a
conversion to a type acceptable to the predefined operator
with
[
T=int
]

Any informative reply will be appreciated, thanks in advance.

Best regards,
Liu Hao

Oct 12 '07 #1
3 1577
Hi Liu Hao,
void operator+(A<Tlh s, A<Trhs) {
This function takes two objects of the A class.
obj + t;
But here you're calling operator+ with one A object and one int.
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'
The compiler is complaining that it doesn't know how to convert an int
into an A<somethingobje ct.

If you used two A objects it would work:

A<intobj(5);
A<intobj2(5);
obj + obj2;

You could also rewrite your + operator so that the object doesn't need
to be an instance of A<something>:

void operator+(A<Tlh s, T rhs)

Or alternatively it may be possible to define an operator that allows an
int to be converted into an A<int- sorry, I'm not sure of the exact
syntax. (I would've thought the constructor would be called implicitly
though - perhaps someone can enlighten us.)

Cheers,
Adam.
Oct 12 '07 #2
On 10 12 , 10 31 , Adam Nielsen <adam.niel...@r emove.this.uq.e du.au>
wrote:
Hi Liu Hao,
void operator+(A<Tlh s, A<Trhs) {

This function takes two objects of the A class.
obj + t;

But here you're calling operator+ with one A object and one int.
>
>I think here the compiler is invoking the constructor A(int t) of class A<int>, thus got an temporary A<intobject.
>
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'

The compiler is complaining that it doesn't know how to convert an int
into an A<somethingobje ct.

If you used two A objects it would work:

A<intobj(5);
A<intobj2(5);
obj + obj2;

You could also rewrite your + operator so that the object doesn't need
to be an instance of A<something>:

void operator+(A<Tlh s, T rhs)

Or alternatively it may be possible to define an operator that allows an
int to be converted into an A<int- sorry, I'm not sure of the exact
syntax. (I would've thought the constructor would be called implicitly
though - perhaps someone can enlighten us.)

Cheers,
Adam.

Oct 12 '07 #3
On Oct 12, 4:31 am, Adam Nielsen <adam.niel...@r emove.this.uq.e du.au>
wrote:
void operator+(A<Tlh s, A<Trhs) {
This function takes two objects of the A class.
This is not a function, but a template. It only becomes a
function when instantiated.
obj + t;
But here you're calling operator+ with one A object and one int.
Here, you're asking the compiler to deduce the types for the
above template, and instantiate it using the deduced types.
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'
The compiler is complaining that it doesn't know how to convert an int
into an A<somethingobje ct.
No. The compiler knows how to convert an int into an A<int>
object, which is what is wanted. The problem here is that the
rules for type deduction do not allow the compiler to deduce the
arguments for the operator+ template, so it cannot instantiate
the function.
If you used two A objects it would work:
A<intobj(5);
A<intobj2(5);
obj + obj2;
You could also rewrite your + operator so that the object
doesn't need to be an instance of A<something>:
void operator+(A<Tlh s, T rhs)
Or alternatively it may be possible to define an operator that
allows an int to be converted into an A<int- sorry, I'm not
sure of the exact syntax. (I would've thought the constructor
would be called implicitly though - perhaps someone can
enlighten us.)
The usual solution here would be something along the lines of:

template< typename T >
class A
: public Operators< A< T
, public MixedOperators< A< T >, T >
{
public:
// ...
A& operator+=( A const& other ) ;
} ;

with the usual definitions for Operators and MixedOperators:

template< typename T >
class Operators
{
public:
friend T operator+( T const& lhs, T const& rhs )
{
T result( lhs ) ;
result += rhs ;
return result ;
}
// And so on for the other operators...
} ;

template< typename T1, typename T2 >
class MixedOperators
{
public:
friend T1 operator+( T1 const& lhs, T2 const& rhs )
{
T1 result( lhs ) ;
result += rhs ; // Note that here, template
// type deduction isn't needed,
// so the compiler will find
// any necessary conversions.
return result ;
}
// And so on for the other operators...
friend T1 operator+( T2 const& lhs, T1 const& rhs )
{
T1 result( rhs ) ;
result += lhs ;
return result ;
}
// And so on for the other commutative operators...
} ;

(Note that in this solution, there are no function
templates:-).)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 12 '07 #4

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

Similar topics

5
2079
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program works fine but there in one thing that I'm unsure about and that is the inheritance statement. What difference is it if I have this construction class CheckedArray : public Array<T>
12
2612
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
5
2336
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and run this works fine. but if build in unicode release or release this does't work. IS THERE ANY PROBLEM OF INSTANTIATING TEMPLATE CLASSES
3
4004
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
1
3070
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
3
2954
by: erictham115 | last post by:
Error C2555 c:\C++ projects\stat1\stdmatrix_adapt.h(41) : error C2555: 'std_tools::Matrix_adapter<T>::at': overriding virtual function return type differs and is not covariant from 'ple::imtx_impl<T>::at' //in my program: the derived class template <class T> class Matrix_adapter : public ple::imtx_impl<T{ protected:
5
2529
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to determining the class type. The code is below:
4
2485
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
1
2373
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear { public:
0
2917
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing this that I'm using partial _function_ specialization. I'm sure I remember reading somewhere that
0
8376
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
8290
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
8815
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
8708
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...
0
7307
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
5622
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2716
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
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
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.