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

Lack of respect for an explicit constructor?


Hello again,

In the program below, the following line is of interest:

bar.insert(make_pair(42, 17));

insert() expects a pair<foo<1>, foo<2> >. I'm giving it a pair <int, int>.
But this is OK because there is an implicit conversion in pair that allows
this:

// This is in the definition of pair; it is a member template that happens
to be a constructor.
template<class U, class V> pair(const pair<U, V> &p);

This will allow conversion from pair<int, int> to pair<foo<1>, foo<2> >.
But it seems that we should now run into a problem...

Each member of the pair has to, in turn, undergo an implicit conversion.
Specifically int to foo<1> and int to foo<2>. How is it that this is
working (VC++ 6.0 and g++)? My constructor in foo that takes an int is
explicit!!! Can anyone shed light on how this compiles and runs as if the
constructor were not explicit?

Thanks,
Dave

P.S. WW: Thanks for clarifying my earlier ridiculous mistake!!!

#ifdef WIN32
#pragma warning(disable: 4786)
#endif

#include <iostream>
#include <map>

using namespace std;

template <int type_differentiator>
class foo
{
public:
explicit foo(int d): data(d)
{
cout << "foo<"
<< type_differentiator
<< ">(int) : "
<< data
<< endl;
}

foo(const foo &f): data(f.data)
{
cout << "foo<"
<< type_differentiator
<< ">(const foo &): "
<< data
<< endl;
}

~foo()
{
cout << "~foo<"
<< type_differentiator
<< ">() : "
<< data
<< endl;
}

bool operator<(const foo &lhs) const {return data < lhs.data;}

private:
int data;
};

int main()
{
map<foo<1>, foo<2> > bar;

cout << "Starting..." << endl;

// Question: How does the line below compile? foo's constructor is
explicit!
bar.insert(make_pair(42, 17));

// bar.insert(make_pair(foo<1>(43), foo<2>(18)));

cout << "Exiting..." << endl;

return 0;
}


Jul 19 '05 #1
2 2958
Dave Theese wrote:
Hello again,

In the program below, the following line is of interest:

bar.insert(make_pair(42, 17));

insert() expects a pair<foo<1>, foo<2> >. I'm giving it a pair <int,
int>. But this is OK because there is an implicit conversion in pair
that allows this:

// This is in the definition of pair; it is a member template that
happens to be a constructor.
template<class U, class V> pair(const pair<U, V> &p); [SNIP] // Question: How does the line below compile? foo's constructor is
explicit!
bar.insert(make_pair(42, 17));

[SNIP]

Post the source of that templated constructor. Without that there is no way
to tell what is going on since it is in there, whatever is going on. For
example you using explicit syntax to make that foo<1> and foo<2>.

--
Attila aka WW
Jul 19 '05 #2
Dave Theese wrote in news:vn************@news.supernews.com:
Each member of the pair has to, in turn, undergo an implicit
conversion. Specifically int to foo<1> and int to foo<2>. How is it
that this is working (VC++ 6.0 and g++)? My constructor in foo that
takes an int is explicit!!! Can anyone shed light on how this
compiles and runs as if the constructor were not explicit?


Because the template constructor initializes explicitly
i.e:

#include <iostream>

struct X
{
int x;
explicit X( int xx ) : x( xx )
{
std::cerr << "explicit X::X\n";
}
};

template < typename F >
struct single
{
F only;
single( F const &rhs ) : only( rhs ) {}
template < typename U >
single( single< U > const &rhs ) : only( rhs.only )
{
std::cerr << "template single::single\n";
}
};

int main()
{
single< int > si( 1 );

std::cerr << "X ...\n";
single< X > sx( si );
}

Note the line: single( single< U > const &rhs ) : only( rhs.only )
and especialy the only( rhs.only ) whitch is the explicit
initialization.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

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

Similar topics

2
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
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;
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
2
by: Dave | last post by:
Hello NG, Can anybody fathom the purpose of an explicit copy constructor? On page 232 of the Josuttis STL reference, I see a reference to such. How could you ever need to supress the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
by: Fred Zwarts | last post by:
Consider the following code: // Start of code class MyException_t { public: // Default constructor.
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.