473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

template classes and implicit conversion

Hi,
I cant understand why this code does not work.
I've implemented two classes A,B that provide an array like structure
(they are identical).
I can convert B in A using implicit conversion, but it works only in
argument passing in non-template function (in this case the commented
sum) but not with the same expressed in template form (uncommented
sum).
This code wont compile, uncommenting the specialized sum function it
would do instead.
the error is " error: no matching function for call to `sum(B<int,
4u>&)' "

There's an explanation at this strange behavior or is a compiler bug
(mine is gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-8) ) ?

Thanks,
Alessandro.

//-----------

template <class T,unsigned int N>
class A;
template <class T,unsigned int N>
class B;
template <class T,unsigned int N>
class A
{
public:
A(){;}
T c[N];
};
template <class T,unsigned int N>
class B
{
public:
B(){;}
operator const A<T,N&(){return *(A<T,N*)this;}
T c[N];
};
template <class T,unsigned int N>
T sum(const A<T,N&a)
{
T s(0);
for (unsigned int i=0;i<N;i++)
s+=a.c[i];
return s;
}

//int sum(A<int,4a)
//{
// int s(0);
// for (unsigned int i=0;i<4;i++)
// s+=a.c[i];
// return s;
//}
int main()
{
A<int,4a;
B<int,4b;

sum(a);
sum(b);
}

Jul 24 '07 #1
0 1399

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

Similar topics

3
by: Kevin Ruland | last post by:
Hi all. I have a template class with conversion operator: template< typename T > class FooWrapper { public: FooWrapper( const T& rhs ); }
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
12
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...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
1
by: flopbucket | last post by:
Hi, If I have: template<class T> class X { ..... };
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
2
by: teel | last post by:
Hi there, I finally wrote a template class for a parameter, which is listed below. I'd like to use the parameter to store int, float or enum object, and to use it with a property, thus I don't...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.