据我所知,std::scoped_allocator_adapter提供了一种控制机制,用于分别指定容器,元素,元素元素等使用哪个分配器,假设元素本身是容器.
也就是说,我无法理解语义std::scoped_allocator_adapter.
Bjarne Stroustrup在"C++编程语言 "第34.4.4节中提供了以下4个例子.1001(我将在我的问题中将它们称为示例-1,示例-2等.):
我们有四种方法来分配字符串向量:
Run Code Online (Sandbox Code Playgroud)// vector and string use their own (the default) allocator: using svec0 = vector<string>; svec0 v0; // vector (only) uses My_alloc and string uses its own allocator (the default): using Svec1 = vector<string,My_alloc<string>>; Svec1 v1 {My_alloc<string>{my_arena1}}; // vector and string use My_alloc (as above): using Xstring = basic_string<char,char_traits<char>, My_alloc<char>>; using Svec2 = vector<Xstring,scoped_allocator_adaptor<My_alloc<Xstring>>>; Svec2 v2 {scoped_allocator_adaptor<My_alloc<Xstring>>{my_arena1}}; // vector uses its own alloctor (the …
请考虑以下代码:
#include <iostream>
#include <string>
using namespace std;
// helpers
void disp1(string s, int i, void* p) { cout << s << " constructed with " << i << " @ " << p << "\n"; }
void disp2(string s, void* p) { cout << s << " @ " << p << "\n"; }
// class hierarchy:
//
// A A
// | |
// B C
// \ /
// D
//
struct A { A() { disp1("A", …Run Code Online (Sandbox Code Playgroud) 考虑下面的代码,其中创建了2种不同类型的组合框(WC_COMBOBOX和WC_COMBOBOXEX),然后每个都附加了一个工具提示.
WC_COMBOBOX的工具提示按预期工作,但WC_COMBOBOXEX无法显示工具提示.
问题是什么?
BOOL TooltipDlg_OnInitDialog(HWND hWndDialog, HWND hWndFocus, LPARAM lParam)
{
// Load and register Tooltip, ComboBox, ComboBoxEx control classes
INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC = ICC_WIN95_CLASSES | ICC_USEREX_CLASSES;
if (!InitCommonControlsEx(&iccx))
return FALSE;
// Create combo boxes
const int idc_ComboBox = 1000;
const int idc_ComboBoxEx = 1001;
{
// create WC_COMBOBOX
CreateWindow(WC_COMBOBOX, NULL,
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
40, 80,
100, 20,
hWndDialog, (HMENU)idc_ComboBox, g_hInst,
NULL);
// create WC_COMBOBOXEX
CreateWindowEx(0, WC_COMBOBOXEX, NULL,
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
40, 110, …Run Code Online (Sandbox Code Playgroud) 关于优先级和评估顺序的概念有很多问题,但我没有找到一个涉及我的特殊情况的问题.
请考虑以下声明:
if(f(0) && g(0)) {};
Run Code Online (Sandbox Code Playgroud)
是否保证首先评估f(0)?请注意,运算符是&&.
我的困惑源于我在"The C++ Programming Language,(Stroustrup,4ed,2013)"中所读到的内容.
在本书的第10.3.2节中,它说:
表达式中子表达式的评估顺序是不确定的.特别是,您不能假设表达式是从左到右计算的.例如:
int x = f(2)+ g(3); // undefined是否先调用f()或g()
这似乎适用于包括 &&运算符在内的所有运算符,但在后面的段落中它说:
运算符,(逗号),&&(逻辑和)和|| (逻辑或)保证在其右侧操作数之前评估其左侧操作数.
第11.1.1节还提到了另一个问题:
&&和|| 运营商仅在必要时评估其第二个参数,因此它们可用于控制评估顺序(第10.3.2节).例如:
while(p &&!whitespace(p))++ p;
这里,如果它是nullptr,则p不被解除引用.
最后一句话暗示了&&和|| 首先评估他们的第一个参数,所以它似乎强化了我的假设,即第二个引用中提到的运算符是第一个引用的例外,但我也无法从最后一个例子中得出明确的结论,因为表达式只包含一个子表达式而不是我的例子,其中包含两个.
我在下面的代码中的目的是能够调用一个以lambda作为参数的虚函数。我的处理方式显然是不对的,因为虚拟模板是不允许的。
有办法解决吗?
// polymorphic base class
struct B {
virtual void set_val(int) = 0;
virtual int get_val() = 0;
virtual void set_next(B*) = 0;
template<typename FCall_T> // compiler error: no virtual templates allowed
virtual void for_each_node_do(FCall_T) = 0;
};
// derived class
struct D :
public B
{
int val;
D* p_next;
void set_val(int i) override { val = i; }
int get_val() override { return val; }
void set_next(B* p_next_)override { p_next = dynamic_cast<D*>(p_next_); };
template<typename FCall_T> …Run Code Online (Sandbox Code Playgroud) 我有一个单线程程序,在非调试模式下运行时,在调用后的某些点上会持续崩溃。free()
然而,在调试模式下,free() 即使没有设置断点,调试器也会在调用的行上中断。当我尝试再次进入下一行时,调试器在同一行再次中断。再次单步恢复正常执行。没有崩溃,没有段错误,什么都没有。
\n\n\nEDIT-1:与我上面写的相反,非调试模式下的崩溃结果是一致的,这让我觉得我在某种程度上写了一些我不应该写的地方。(不过,调试模式下的中断仍然是一致的。)
\n
中断处的调用堆栈显示了在调用语句的函数之后调用的一些 Windows 库函数(我认为)free()。我不知道如何解释它们。因此,我不知道在这种情况下如何进行调试。
我在下面的断点处提供了调用堆栈。有人可以指出我可以解决问题的方向吗?什么可能导致调试器模式中断?
\n\n程序在Windows Vista上运行,使用gcc 4.9.2编译,使用的调试器是gdb。假设双重释放不是这种情况。(我使用::operator new和::operator delete重载来捕获这一点。描述的情况在没有这些重载的情况下也是相同的。)
请注意,崩溃(或调试器中的非自愿中断)是一致的。每次都会在同一个执行点发生。
\n\n这是初始中断时的调用堆栈:
\n\n(请注意,free_wrapper()该函数包含free()导致崩溃/中断的语句。)
#0 0x770186ff ntdll!DbgBreakPoint() (C:\\Windows\\system32\\ntdll.dll:??)\n#1 0x77082edb ntdll!RtlpNtMakeTemporaryKey() (C:\\Windows\\system32\\ntdll.dll:??)\n#2 0x7706b953 ntdll!RtlImageRvaToVa() (C:\\Windows\\system32\\ntdll.dll:??)\n#3 0x77052c4f ntdll!RtlQueryRegistryValues() (C:\\Windows\\system32\\ntdll.dll:??)\n#4 0x77083f3b ntdll!RtlpNtMakeTemporaryKey() (C:\\Windows\\system32\\ntdll.dll:??)\n#5 0x7704bcfd ntdll!EtwSendNotification() (C:\\Windows\\system32\\ntdll.dll:??)\n#6 …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我有一个带有成员类型(struct Element)的模板类.我想为该成员类型重载operator << .但是,代码不会编译.
如果有人能指出我哪里出错了,我将不胜感激?
include <iostream>
using namespace std;
// fwd decl
template<class T> class CC;
// operator<< overload template for member type CC<T>::Element
template<class T>
ostream& operator<<(ostream& os, const typename CC<T>::Element& elm) {
return os << elm.val1 << "/" << elm.val2;
}
template<class T>
class CC
{
public:
struct Element { // type member
int val1;
int val2;
};
template<typename U>
friend ostream& operator<<(ostream& os, const typename CC<U>::Element& elm);
};
int main() {
CC<int>::Element …Run Code Online (Sandbox Code Playgroud) 是否有可能避免在以下头文件循环依赖而不转动数据成员B1中A类的指针/引用,而不放松在直列式功能需求B类?
啊:
#ifndef A_H
#define A_H
#include <B.h> // Required, as data member b1 is not a pointer/reference
class A {
public:
B b1; // I want to keep this as as it is.
int m_a;
};
#endif
Run Code Online (Sandbox Code Playgroud)
Bh:
#ifndef B_H
#define B_H
#include <A.h> // Required, as f() calls a member function of class A
class B {
public:
int f(A &a){return a.m_a;} // I want this to be …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码片段,其中WM_TIMER消息上显示消息框.
#define IDT_TIMER1 1001
INT_PTR CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch(message) {
//...
case WM_INITDIALOG:
{
//...
SetTimer(hWnd, IDT_TIMER1, 1000, (TIMERPROC)NULL);
break;
}
case WM_TIMER:
{
int ret = MessageBox(hWnd, L"Cancel operation?", NULL, MB_YESNO);
if(ret == IDYES) {
EndDialog(hWnd, 0);
} else {
// no-op: keep going
}
break;
}
//...
default:
return FALSE;
}
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
我希望此代码在初始计时器时显示一个消息框,并阻止该线程,直到用户单击一个按钮.什么实际情况是,一个新的消息框显示在每个时钟滴答,即使用户并没有点击任何按钮.
当我检查线程的调用堆栈时,我看到多个调用DialogProc(),所有调用都在线MessageBox()调用(即所有等待用户输入).
鉴于调用堆栈的状态,它怎么可能DialogProc()不断得到所谓的在 …
c++ ×9
templates ×2
winapi ×2
allocator ×1
c++11 ×1
combobox ×1
controls ×1
debugging ×1
header-files ×1
lambda ×1
polymorphism ×1
std ×1
tooltip ×1
type-members ×1
windows ×1