D
Dave Theese
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;
}
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;
}