Let us consider the following examples of where implicit type conversion works and where it doesn't:
#include <iostream>
#include <vector>
struct Thingy
{
void write()
{
std::cout << "x" << std::endl;
}
};
struct Node
{
Thingy a;
int data;
operator Thingy&(){return a;}
};
void f(Thingy thingy)
{
thingy.write();
}
template <typename TIterator>
void f (TIterator begin, TIterator end)
{
for (TIterator it = begin; it != end; ++it)
it->write();
}
int main()
{
std::vector<Node> vector(10);
f(vector.begin(), vector.end()); // Doesn't compile
f(vector[3]); // compiles
vector[3].write(); // Doesn't compile
return 0;
}
Why is this so? The
void Node::write();
Should not be fundamentally different from:
void write(Node* this);
Is there any way to make my example code compile and run?
EDIT:
I understand the mechanics of why it doesn't work, I want to understand the philosophy. Why did the C++ Standard committee think it was a bad Idea?
Nodedoesn't have awritefunction.