小编Ang*_*tis的帖子

什么是C++中未评估的上下文?

经常浮现的一个例子是:

sizeof表达式,它不计算表达式,但通过静态类型确定大小.例如 :

int func();
sizeof(func());
Run Code Online (Sandbox Code Playgroud)

这是我思考的极限,所以如果还有其他未评估的背景,那么它们是什么?

c++ c++11

44
推荐指数
2
解决办法
2457
查看次数

具有多个功能和多个转换运算符的过载分辨率

考虑简单的代码:

#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

13
推荐指数
2
解决办法
348
查看次数

什么是C++中的类型擦除?

所以我正在读这篇关于类型擦除的文章.但该文章中的代码似乎部分不正确,例如:

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工作的,但无法理解它.

c++ type-erasure

11
推荐指数
1
解决办法
1794
查看次数

过载分辨率和用户定义的转换

考虑一下简单的代码:

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)已选定,由于参数funcA它是类的类型,因此需要转换的类型是"用户定义的转换序列"

现在来自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&)- …

c++ overload-resolution

8
推荐指数
1
解决办法
391
查看次数

为什么在这种情况下重载解析是不明确的?

我编写了这段代码来检查类类型是否具有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)

c++ sfinae

5
推荐指数
1
解决办法
496
查看次数

为什么显式调用operator << ambiguous?

这是简单的代码:

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;?谢谢

c++ iostream

2
推荐指数
1
解决办法
156
查看次数

std :: forward的使用比较

考虑简单的代码(免责声明:这是一个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但我特别想知道这两个案例.两个版本完全相同,还是一个比另一个更好?

c++ c++11

1
推荐指数
1
解决办法
91
查看次数