473,659 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<in t>&)'

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<typena me T>
class C
{
public:
C() { }
C(const C&) { }
};

template<typena me 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 2312
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<in t>&)'

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<typena me T>
class C
{
public:
C() { }
C(const C&) { }
};

template<typena me 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
1860
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 my_template
6
7510
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
4233
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
8
2874
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 (const T &num); // Conversion constructor
10
4015
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
10
3168
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 ScreenXCoord; explicit typedef unsigned int ScreenYCoord; explicit typedef unsigned int WindowXCoord; explicit typedef unsigned int WindowYCoord;
6
1568
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 appears to think that it actually calls one of the *other* constructors. The copy ctor would work in this context, but the other ctor can't be used (it leads to a template instantiation error, which the compiler is reporting).
2
2161
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 the default ctor. Suppose we try to create Test obj;
9
2887
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 Foo() is included, i believe. };
0
8339
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
8851
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
8751
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
8535
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
8629
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4176
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...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
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
2
1739
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.