473,378 Members | 1,493 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,378 software developers and data experts.

explicit copy ctor in template class : compiler error. why?

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. X<Zx(y) is fine.
Tested with gcc 2.95, 3.3, 4.1, all gave the same error:

t.cpp: In function 'int main()':
t.cpp:44: error: no matching function for call to
'D<int>::D(D<int>&)'

which is quite right, since there is only D(const D&). the thing i
don't understand is why this error only occures if the class is a
template, and if used as D<intd3 = d, since D<intd2(d) compiles
fine.

is there a difference between the variable declarations A a(b) and A a
= b? to the best of my knowledge, not, and the compilers seem to
generate the same code for both.

and for the ones asking why i qualify the copy ctor 'explicit' : it's
not needed, but all other ctors were explicit, so i thought, it look
consistent that way, then ran into this issue.

regards, p
here's the code:
class A
{
public:
A() { }
A(const A&) { }
};

class B
{
public:
explicit B() { }
explicit B(const B&) { }
};

template<typename T>
class C
{
public:
C() { }
C(const C&) { }
};

template<typename T>
class D
{
public:
explicit D() { }
explicit D(const D&) { }
};
int main()
{
A a;
B b;
C<intc;
D<intd;

A a2(a);
B b2(b);
C<intc2(c);
D<intd2(d);
C<intc3 = c;
D<intd3 = d; // <--- compiler error

return 0;
}

Dec 15 '06 #1
1 2282
petschy wrote:
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. X<Zx(y) is fine.
Tested with gcc 2.95, 3.3, 4.1, all gave the same error:

t.cpp: In function 'int main()':
t.cpp:44: error: no matching function for call to
'D<int>::D(D<int>&)'

which is quite right, since there is only D(const D&). the thing i
don't understand is why this error only occures if the class is a
template, and if used as D<intd3 = d, since D<intd2(d) compiles
fine.

is there a difference between the variable declarations A a(b) and A a
= b? to the best of my knowledge, not, and the compilers seem to
generate the same code for both.
Clearly, you don't know what the keyword "explicit" is supposed to
mean. You are right that there is no difference between those
declarations... /except/ that one involves an explicit use of the copy
constructor and the other an implicit use.

Your problem also has nothing to do with the fact that D<is a
template. It would've given you the same error had you used B in that
way.
and for the ones asking why i qualify the copy ctor 'explicit' : it's
not needed, but all other ctors were explicit, so i thought, it look
consistent that way, then ran into this issue.
Using "explicit" because that's consistent with how the other
constructors in the same class are declared, is a very poor reason. In
particular, using the "explicit" keyword in constructors that require
more than one argument (or accept no arguments) would be utterly
useless.

You shouldn't use the "explicit" keyword unless you mean it: even if
you have other constructors accept a single argument, and they're all
explicit so far, that doesn't mean you need to make another one
explicit. It should be on a case-by-case basis.

Copy constructors, in particular, should usually not be explicit unless
you have a specific reason to disallow "assignment"-style declarations,
and automatic conversions for (say) function arguments.

For instance, if you had a class you were calling Integer, you would
probably want to make conversions from double explicit, to make sure
the invoker is paying attention to what they're doing, whereas you'd
probably want to leave conversions from plain int implicit.

The std::vector class has a constructor that is capable of taking a
single integer argument (specifying the initial capacity). That one is
declared to be explicit, since a declaration like:

std::vector a = 15;

is not very intuitive at all, and a vector conversion from an integer
function argument would be downright confusing.

A general rule-of-thumb is that, if there is a loss of information in
the conversion (as in the case of double-to-Integer), it should be
explicit. Another good rule is that if an implicit conversion could be
confusing, disallow it.
>
regards, p
here's the code:
class A
{
public:
A() { }
A(const A&) { }
};

class B
{
public:
explicit B() { }
explicit B(const B&) { }
};

template<typename T>
class C
{
public:
C() { }
C(const C&) { }
};

template<typename T>
class D
{
public:
explicit D() { }
explicit D(const D&) { }
};
int main()
{
A a;
B b;
C<intc;
D<intd;

A a2(a);
B b2(b);
C<intc2(c);
D<intd2(d);
C<intc3 = c;
D<intd3 = d; // <--- compiler error

return 0;
}
Dec 15 '06 #2

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

Similar topics

1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
5
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
8
by: NVH | last post by:
I know that this question may have been asked before but I can't find it here so: If there is a class: class Foo { ... Foo (const Foo &num); // Copy constructor template <typename T> Foo...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
10
by: jlongstreet | last post by:
Please correct any misconceptions, or voice concerns about this being a stupid idea in general. I was wondering today: why doesn't C++ have explicit typedefs? explicit typedef unsigned int...
6
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler...
2
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.