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) #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)
都很好.
为什么转发引用在这种情况下不起作用?
考虑:
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
成员函数可以?
我搜索了互联网和stackoverflow.com,但什么也没得到.
谁可以给我比较各种Prolog编译器/ IDE,如Visual Prolog和SWI-Prolog?
提前致谢!
下面的代码被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)
为什么?
#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
接受一个狭窄的字符串文字作为其参数但不接受一个狭窄的字符串对象?
以下代码是使用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可以是异常的.
所以,我的问题是:
什么是C++ lambda函数的调用约定?
如何指定C++ lambda函数的调用约定?
如果没有定义调用约定,那么在调用lambda函数后如何正确地回收堆栈空间?
编译器是否自动生成lambda函数的多个版本?即作为以下伪代码:
[] __stdcall(){};
[] __cdecl(){}; 等等
以下程序是使用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;
那么程序按预期运行,没有例外.
为什么?
#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++中的数组?
根据cppref:
std::allocator<T>::allocate_at_least
通过调用 (可能提供附加参数)分配
count * sizeof(T)
未初始化存储的字节,其中count
是不小于 的未指定整数值,但未指定何时以及如何调用此函数。n
::operator new
std::align_val_t
T[count]
然后,该函数在存储中创建一个类型数组并开始其生命周期,但不会启动其任何元素的生命周期。
然而,我认为现有的std::allocator<T>::allocate
可以做同样的事情。
为什么我们需要 std::allocator<T>::allocate_at_least
C++23?
c++ ×9
c++11 ×4
standards ×4
templates ×2
algorithm ×1
allocator ×1
c ×1
c++23 ×1
constructor ×1
destructor ×1
encoding ×1
exception ×1
function ×1
inheritance ×1
iostream ×1
lambda ×1
oop ×1
overloading ×1
performance ×1
polymorphism ×1
prolog ×1
string ×1
types ×1
virtual ×1