В C ++ допускается следующее продвижение:
int ivalue = true;
bool bvalue = 1;
Это хорошо. И это не допускается через проверку типов:
int& ivalue = false;
bool& bvalue = 0;
Это нормально.
Посмотри на это из Википедии.
http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B
#include <iostream>
template <typename T> class property {
T value;
public:
T & operator = (const T &i) {
::std::cout << "T1: " << i << ::std::endl;
return value = i;
}
// This template class member function template serves the purpose to make
// typing more strict. Assignment to this is only possible with exact identical
// types.
template <typename T2> T2 operator = (const T2 &i) {
::std::cout << "T2: " << i << ::std::endl;
T2 &guard = value;
return value = i;
throw guard; // Never reached.
}/**/
operator T const & () const {
return value;
}
};
struct Bar {
// Using the property<>-template.
property <bool> alpha;
property <unsigned int> bravo;
};
int main () {
Bar bar;
bar.alpha = true;
bar.bravo = true; // This line will yield a compile time error
// due to the guard template member function.
::std::cout << foo.alpha << ", "
<< foo.bravo << ", "
<< bar.alpha << ", "
<< bar.bravo
<< ::std::endl;
bool bvar = 22;
int ivar = true;
//int &newvar = bvar;
print(bvar);
::std::cout << bvar << " and " << ivar << "\n";
return 0;
}
Я думаю, с использованием шаблонов проверка типов для ссылок теряется? Я прав?