[英]Have no idea about C++11 type inference
Have no idea about C++11 type inference 对C ++ 11类型推断一无所知
As I known, there are at least 3 type inferences in C++11: 如我所知,C ++ 11中至少有3种类型推断:
But I can't build a concept model for them. 但是我不能为他们建立一个概念模型。 It makes me confused.
这让我感到困惑。
That results in that I don't know what is right in subtle case. 结果导致我不知道在什么情况下是正确的。
In fact, I don't even know what my question is. 实际上,我什至不知道我的问题是什么。 But, I try:
但我会尝试:
I want to know how cv, & and && qualifiers affect the type inference. 我想知道cv,&和&&&限定词如何影响类型推断。
I want to know what the difference is between the three kinds of type inference. 我想知道三种类型推断之间的区别。
///The following extract from 14.8.2.1 in n3242
template <class T> int f(T&&);
template <class T> int g(const T&&);
int i;
int n1 = f(i); // calls f<int&>(int&)
int n2 = f(0); // calls f<int>(int&&)
int n3 = g(i); // error: would call g<int>(const int&&), which
// would bind an rvalue reference to an lvalue
///The following extract from 8.3.2 in n3242
int i;
typedef int& LRI;
typedef int&& RRI;
LRI& r1 = i; // r1 has the type int&
const LRI& r2 = i; // r2 has the type int&
const LRI&& r3 = i; // r3 has the type int&
RRI& r4 = i; // r4 has the type int&
/*The following statement encounter compilation error in gcc 4.6:error message:
invalid initialization of reference of type int&& from expression of type int*/
RRI&& r5 = i; // r5 has the type int&&
decltype(r2)& r6 = i; // r6 has the type int&
decltype(r2)&& r7 = i; // r7 has the type int&
///The following is from some blog
int i;
decltype( i ) ==> int
decltype( (i) ) ==> int &
Template deduction is in C++03 模板推导在C ++ 03中
template <typename T> void foo(T) {}
int i;
float f;
foo (i); // deduces foo<int>
foo (f); // deduces foo<float>
Here the compiler sees foo(i)
and says to itself "the T
part of foo
has to be an int
for this to match". 在这里,编译器看到
foo(i)
并自言自语“ foo
的T
部分必须是一个int
才能与之匹配”。
auto
is pretty simple. auto
非常简单。
int foo ();
float bar ();
auto i = foo (); // i is an int
auto f = bar (); // f is a float
The compiler sees auto i =
and says to itself "well the right hand side yields an int
so i
will have to be one of those". 编译器看到
auto i =
并对自己说:“右边会产生一个int
所以i
必须成为其中之一”。
decltype
is a bit more involved, a kind of meta-auto. decltype
有点复杂,是一种meta-auto。 decltype(x)
is equivalent to int
if x
is an int
, float
if x
is a float
, etc. The advantage is that you can use it in template expressions. decltype(x)
等价于int
,如果x
是一个int
, float
若x
是一个float
等的优点是,你可以在模板表达式中使用它。
int foo (float);
float foo (int);
template <typename T> void convert (std :: vector <T> input) {
std :: vector <decltype (foo(input[0]))> output;
output .push_back (foo (input [0])); // yeah, ok, not safe, meh
}
convert (std :: vector <int> ()); // Will create an output of type std::vector<float>
convert (std :: vector <float> ()); // Will create an output of type std::vector<int>
Here decltype (foo(input[0]))
is float
when input
is a vector of int
because input[0]
is an int
and the overload of foo
which takes an int
returns a float
. 当
input
是int
的向量时,此处decltype (foo(input[0]))
是float
,因为input[0]
是int
,而采用int
的foo
重载返回float
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.