小编xml*_*lmx的帖子

sizeof(size_t)== sizeof(void*)总是如此吗?

C99/C++ 11标准是否保证sizeof(size_t) == sizeof(void*)始终如一?

size_t f(void* p)
{
    return (size_t)(p); // Is it safe?
}

void* f(size_t n)
{
    return (void*)(n); // Is it safe?
}
Run Code Online (Sandbox Code Playgroud)

c c++ standards compatibility types

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

在这种情况下,为什么不转发参考工作?

#include <vector>

using namespace std;

template<typename T, typename = decltype(&T::size)>
void f1(T)
{}

template<typename T, typename = decltype(&T::size)>
void f2(T&)
{}

template<typename T, typename = decltype(&T::size)>
void f3(T&&)
{}

int main()
{
    vector<int> coll;

    f1(coll); // ok
    f2(coll); // ok
    f3(coll); // error : no matching function for call to 'f3'
}
Run Code Online (Sandbox Code Playgroud)

main.cpp(21,6):注意:候选模板被忽略:替换失败[with T=> std::vector<int, std::allocator<int> > &]:类型' std::vector<int, std::allocator<int> > &'在' ::' 之前不能使用,因为它没有成员

void f3(T&&)

我的编译器是clang 4.0.

令我惊讶的是,f3(coll)失败了,f1(coll)而且f2(coll)都很好.

为什么转发引用在这种情况下不起作用?

c++ templates overloading c++11 forwarding-reference

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

为什么不能公开继承的受保护构造函数?

考虑:

class A
{
protected:
    A(int) {}
    void f(int) {}

public:
    A() {}
};

class B : public A
{
public:
    using A::A;
    using A::f;
};

int main()
{
    B().f(1); // ok
    B(1); // error: 'A::A(int)' is protected within this context
}
Run Code Online (Sandbox Code Playgroud)

为什么不能创建继承的protected构造函数public,而继承的protected成员函数可以?

c++ oop inheritance constructor c++11

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

哪个是Visual Prolog,SWI-Prolog和其他人中最好的?

我搜索了互联网和stackoverflow.com,但什么也没得到.

谁可以给我比较各种Prolog编译器/ IDE,如Visual Prolog和SWI-Prolog?

提前致谢!

prolog

16
推荐指数
2
解决办法
9323
查看次数

函数类型可以是类模板参数吗?

下面的代码被VC++ 2012拒绝,"错误C2207:'A :: bar':类模板的成员无法获取函数类型".

int Hello(int n)
{
    return n;
}

template<class FunctionPtr>
struct A
{
    A(FunctionPtr foo)
        : bar(foo)
    {}

    FunctionPtr bar;
};

int main()
{
    A<decltype(Hello)> a(Hello);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么?

c++ templates c++11

16
推荐指数
2
解决办法
3644
查看次数

为什么wcout <<""; 没问题,但是wcout << string(); 不是?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    wcout << L"Hello";          // OK.
    wcout << wstring(L"Hello"); // OK.
    wcout << "Hello";           // OK. Why?
    wcout << string("Hello");   // Error. Why?
}
Run Code Online (Sandbox Code Playgroud)

为什么std::wcout接受一个狭窄的字符串文字作为其参数但不接受一个狭窄的字符串对象?

c++ string encoding iostream

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

什么是C++ lambda函数的默认调用约定?

以下代码是使用VC++ 2012编译的:

void f1(void (__stdcall *)())
{}

void f2(void (__cdecl *)())
{}

void __cdecl h1()
{}

void __stdcall h2()
{}

int main()
{
    f1(h1); // error C2664
    f2(h2); // error C2664

    f1([](){}); // OK
    f2([](){}); // OK

    auto fn = [](){};

    f1(fn); // OK
    f2(fn); // OK
}
Run Code Online (Sandbox Code Playgroud)

我认为错误是正常的,但OK可以是异常的.

所以,我的问题是:

  1. 什么是C++ lambda函数的调用约定?

  2. 如何指定C++ lambda函数的调用约定?

  3. 如果没有定义调用约定,那么在调用lambda函数后如何正确地回收堆栈空间?

  4. 编译器是否自动生成lambda函数的多个版本?即作为以下伪代码:

    [] __stdcall(){};

    [] __cdecl(){}; 等等

c++ lambda function calling-convention c++11

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

如果比较函数不是运算符<?,为什么std :: sort会崩溃?

以下程序是使用VC++ 2012编译的.

#include <algorithm>

struct A
{
    A()
        : a()
    {}

    bool operator <(const A& other) const
    {
        return a <= other.a;
    }

    int a;
};

int main()
{
    A coll[8];
    std::sort(&coll[0], &coll[8]); // Crash!!!
}
Run Code Online (Sandbox Code Playgroud)

如果我return a <= other.a;改为return a < other.a;那么程序按预期运行,没有例外.

为什么?

c++ algorithm standards exception

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

为什么多态不适用于C++中的数组?

#include <iostream>

using namespace std;

struct Base
{
    virtual ~Base()
    {
        cout << "~Base(): " << b << endl;
    }

    int b = 1;
};

struct Derived : Base
{
    ~Derived() override
    {
        cout << "~Derived(): " << d << endl;
    }

    int d = 2;
};

int main()
{
    Base* p = new Derived[4];
    delete[] p;
}
Run Code Online (Sandbox Code Playgroud)

输出如下:(带有Clang 3.8的Visual Studio 2015)

~Base(): 1
~Base(): 2
~Base(): -2071674928
~Base(): 1
Run Code Online (Sandbox Code Playgroud)

为什么多态不适用于C++中的数组?

c++ polymorphism virtual standards destructor

16
推荐指数
2
解决办法
1179
查看次数

为什么在 C++23 中 allocate_at_least() ?

根据cppref

std::allocator<T>::allocate_at_least

通过调用 (可能提供附加参数)分配count * sizeof(T)未初始化存储的字节,其中 count是不小于 的未指定整数值,但未指定何时以及如何调用此函数。n::operator newstd::align_val_t

T[count]然后,该函数在存储中创建一个类型数组并开始其生命周期,但不会启动其任何元素的生命周期。

然而,我认为现有的std::allocator<T>::allocate可以做同样的事情。

为什么我们需要 std::allocator<T>::allocate_at_least C++23?

c++ performance standards allocator c++23

16
推荐指数
2
解决办法
1065
查看次数