经常浮现的一个例子是:
sizeof表达式,它不计算表达式,但通过静态类型确定大小.例如 :
int func();
sizeof(func());
Run Code Online (Sandbox Code Playgroud)
这是我思考的极限,所以如果还有其他未评估的背景,那么它们是什么?
考虑简单的代码:
#include<iostream>
struct A {
operator double(){
std::cout<<"Conversion function double chosen."<<std::endl;
return 1.1;
}
operator char(){
std::cout<<"Conversion function char chosen."<<std::endl;
return 'a';
}
} a;
void foo(int){}
void foo (char){}
int main() {
foo(a);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,正如预期的gcc,clang和VC++选择的那样foo(char).
现在让我们稍微修改一下代码:
#include<iostream>
struct A {
operator double(){
std::cout<<"Conversion function double chosen."<<std::endl;
return 1.1;
}
operator char(){
std::cout<<"Conversion function char chosen."<<std::endl;
return 'a';
}
} a;
void foo(int){}
void foo (double){} //parameter changed from char to double
int main() {
foo(a); …Run Code Online (Sandbox Code Playgroud) c++ overloading conversion-operator language-lawyer overload-resolution
所以我正在读这篇关于类型擦除的文章.但该文章中的代码似乎部分不正确,例如:
template <typename T>
class AnimalWrapper : public MyAnimal
{
const T &m_animal;
public:
AnimalWrapper(const T &animal)
: m_animal(animal)
{ }
const char *see() const { return m_animal.see(); }
const char *say() const { return m_animal.say(); }
};
Run Code Online (Sandbox Code Playgroud)
其次是
void pullTheString()
{
MyAnimal *animals[] =
{
new AnimalWrapper(Cow()), /* oO , isn't template argument missing? */
....
};
}
Run Code Online (Sandbox Code Playgroud)
这些错误使我不鼓励在文章中进一步阅读.
无论如何; 有没有人可以用简单的例子教C++中哪种类型的擦除?
我想了解它是如何std::function工作的,但无法理解它.
考虑一下简单的代码:
struct A;
struct B {
B(){}
B(A const&){ }
};
struct A {
operator int() const {return 0;};
};
void func(B){}
void func(char){}
int main()
{
func(A()); //ambiguous call oO
}
Run Code Online (Sandbox Code Playgroud)
首先,我不确定我是否理解正确的一切,所以当你发现我错了时请纠正我.
我的理解是,该void func(B)已选定,由于参数func是A它是类的类型,因此需要转换的类型是"用户定义的转换序列"
现在来自IBM C++ ref:
用户定义的转换序列包括以下内容:
- 标准转换序列
- 用户定义的转换
- 第二个标准转换序列
现在有两个用户定义的转换存在
B::B(const A&)和A::operator int (const A&);
所以顺序是
- > A()- > B::B(const A&)- >Standard conversion (identity conversion)
- > A()- > A::operator int (const A&)- …
我编写了这段代码来检查类类型是否具有begin函数.
struct foo //a simple type to check
{
int begin(){ return 0;}
};
struct Fallback
{
int begin(){ return 0;}
};
template<typename T>
struct HasfuncBegin : T,Fallback
{
typedef char one;
typedef int two;
template<typename X>
static one check(int (X::*)() = &HasfuncBegin<T>::begin);
template<typename X>
static two check(...);
enum :bool {yes = sizeof(check<T>())==1, no= !yes};
};
int main()
{
std::cout<< HasfuncBegin<foo>::yes;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哪会产生错误:
error: call of overloaded 'check()' is ambiguous
enum {yes = sizeof(check<T>())==1, no= !yes}; …Run Code Online (Sandbox Code Playgroud) 这是简单的代码:
int main()
{
int x=0;
std::cout<<x;
operator<<(std::cout,x); //ambiguous
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么operator<<(std::cout,x)电话不明确但不是std::cout<<x;?谢谢
考虑简单的代码(免责声明:这是一个noobie问题):
template<typename T> struct foo
{
foo(const T&);
foo(T&& ctorArguement):ptrToSomeType(new someType(std::forward<T&&>(ctorArguement))){}
^^
std::unique_ptr<someType> ptrToSomeType;
}
Run Code Online (Sandbox Code Playgroud)
与此相比:
template<typename T> struct foo
{
foo(const T&);
foo(T&& ctorArguement):ptrToSomeType(new someType(std::forward<T>(ctorArguement))){}
^^
std::unique_ptr<someType> ptrToSomeType;
}
Run Code Online (Sandbox Code Playgroud)
我想我应该使用std :: move但我特别想知道这两个案例.两个版本完全相同,还是一个比另一个更好?