C++ 不知道C++;11型推理

C++ 不知道C++;11型推理,c++,c++11,type-inference,C++,C++11,Type Inference,不知道C++11类型推理 正如我所知,在C++11中至少有3种类型推断: 模板推导 自动的 脱模 但我不能为他们建立一个概念模型。这让我很困惑。 这导致我不知道在微妙的情况下什么是正确的 事实上,我甚至不知道我的问题是什么。 但是,我尝试: 我想知道cv、&和&&限定符如何影响类型推断。 我想知道这三种类型推理之间的区别 ///The following extract from 14.8.2.1 in n3242 template <class T> int f(T&&

不知道C++11类型推理

正如我所知,在C++11中至少有3种类型推断:

  • 模板推导
  • 自动的
  • 脱模
但我不能为他们建立一个概念模型。这让我很困惑。
这导致我不知道在微妙的情况下什么是正确的

事实上,我甚至不知道我的问题是什么。 但是,我尝试:

我想知道cv、&和&&限定符如何影响类型推断。
我想知道这三种类型推理之间的区别

///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 &
///n3242中14.8.2.1的以下摘录
模板int f(T&&);
模板int g(常数T&&);
int i;
int n1=f(i);//调用f(int&)
int n2=f(0);//调用f(int&)
int n3=g(i);//错误:将调用g(const int&&),其中
//将右值引用绑定到左值
///n3242中8.3.2的以下摘录
int i;
typedef int&LRI;
typedef int&RRI;
LRI&r1=i;//r1的类型为int&
常数LRI&r2=i;//r2的类型是int&
常数LRI&&r3=i;//r3的类型为int&
RRI&r4=i;//r4的类型为int&
/*以下语句在gcc 4.6中遇到编译错误:错误消息:
从int类型的表达式初始化int类型的引用无效(&R)*/
RRI&&r5=i;//r5的类型是int&&
decltype(r2)&r6=i;//r6的类型为int&
decltype(r2)和&r7=i;//r7的类型为int&
///以下是一些博客上的内容
int i;
decltype(i)==>int
decltype((i))==>int&

模板演绎在C++03中

template <typename T> void foo(T) {}
int i;
float f;
foo (i); // deduces foo<int>
foo (f); // deduces foo<float>
编译器看到
auto i=
并对自己说“好吧,右边会产生
int
,所以
i
必须是其中之一”

decltype
更为复杂,是一种元自动
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>
intfoo(浮点);
浮球(国际);
模板无效转换(标准::矢量输入){
std::矢量输出;
output.push_back(foo(输入[0]);//是的,好的,不安全,meh
}
转换(std::vector());//将创建std::vector类型的输出
转换(std::vector());//将创建std::vector类型的输出

这里
decltype(foo(input[0]))
float
input
int
的向量时,因为
input[0]
int
的一个
foo
的重载取
int
返回一个
float

,这是多么遗憾啊。。。有人能给我一些提示吗?“我甚至不知道我的问题是什么”好吧,很好。1年会员,7个问题,5票,33%被接受,你真的希望有人付出一些努力吗?
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>