我正在尝试将对象存储在std :: set中.这些对象是来自python环境的boost :: shared_ptr <>.向集合中添加值不会导致任何麻烦.但是当我尝试擦除一个值时,即使我传递了相同的引用,它也无法工作.这是一个例子:
#include <set>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;
struct Bar
{
Bar() {}
};
struct Foo
{
set< shared_ptr<Bar> > v_set;
shared_ptr<Bar> v_ptr;
Foo() {}
void add( shared_ptr<Bar> v_param ) {
cout << "storing " << v_param << "in v_set and v_ptr" << endl;
v_set.insert(v_param);
v_ptr = v_param;
}
void del( shared_ptr<Bar> v_param ) {
cout << "deleting " << v_param << endl;
if (v_param …Run Code Online (Sandbox Code Playgroud) 这是我的问题:
我有一个庞大的代码库,所有的类名都以Agui开头.例如:
class AguiWidget
{
};
class AguiBitmap
{
};
Run Code Online (Sandbox Code Playgroud)
另外,所有的hpp和cpp文件也都这样命名:AguiWidget.hpp
等等
该库也不使用命名空间.
所有的枚举开始并使用Agui:ex:
enum AguiKeyEnum
{
AGUI_KEY_SPACE,
AGUI_KEY_ENTER
};
Run Code Online (Sandbox Code Playgroud)
包含警戒也使用ex:AGUI_WIDGET_HPP
所以,
我的任务是从整个项目(所有类和枚举)中删除所有Agui,AGUI引用,然后将所有类封装到命名空间agui中.
最简单的方法是什么?我还需要hpp和cpp文件,文件名中不再包含Agui.
如果有帮助,我使用MSVC 2008作为IDE.
谢谢!
我有一个带有模板参数的类,它应该决定它包含哪两种数据样式.基于该参数,我想以两种不同的方式实现成员函数.我尝试使用Boost Enable-If,但没有成功.这是我最惊讶的代码版本不起作用:
#include <boost/utility/enable_if.hpp>
enum PadSide { Left, Right };
template <int> struct dummy { dummy(int) {} };
template <PadSide Pad>
struct String
{
typename boost::enable_if_c<Pad == Left, void>::type
getRange(dummy<0> = 0) {}
typename boost::enable_if_c<Pad == Right, void>::type
getRange(dummy<1> = 0) {}
};
int main()
{
String<Left> field;
field.getRange();
}
Run Code Online (Sandbox Code Playgroud)
为此,g ++ 4.6.0说:
no type named ‘type’ in ‘struct boost::enable_if_c<false, void>’
Run Code Online (Sandbox Code Playgroud)
当然,第二次重载应该不起作用,但是由于SFINAE,它应该被忽略.如果我删除虚函数参数,g ++会这样说:
‘typename boost::enable_if_c<(Pad == Right), void>::type
String<Pad>::getRange()‘
cannot be overloaded with
‘typename boost::enable_if_c<(Pad == Left), void>::type
String<Pad>::getRange()‘
Run Code Online (Sandbox Code Playgroud)
这就是为什么我把伪参数放在那里 …
在系统编程中,通常会调用某些可能失败的库函数,如果失败,则检查errno确切原因.即使在Python中也是如此,我认为它比它需要的更麻烦.让我们以一些试图删除文件的代码为例,如果文件不存在则继续静默:
try:
sftp.unlink(path)
except IOError as ex:
if ex.errno != errno.ENOENT:
raise
Run Code Online (Sandbox Code Playgroud)
我想知道在Python中是否曾经允许或建议它做更像这样的事情:
try:
sftp.unlink(path)
except IOError as ex if ex.errno == errno.ENOENT:
pass
Run Code Online (Sandbox Code Playgroud)
我认为这有一些建议:
如果之前没有考虑到这一点,我会感到惊讶,所以我会接受任何过去或未决提案的链接.我也接受一个答案,解释为什么上面会引入现有语言的任何问题(Python 3.x,因为我认为2.x主要是冻结的).
我有一个C++容器,我想运行一个循环的次数与该容器中的元素相同.但我不关心循环期间容器中的值.例如:
for (const auto& dummy : input) {
cout << '.';
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,dummy是一个未使用的变量,我已指示编译器禁止这些.
我提出的两个不优雅的解决方案是(void)dummy;在循环体中说要使编译器静音,或者使用从0到0的旧式for循环distance(begin(input), end(input)).
我尝试省略变量名但无法编译(没什么大惊喜).
我正在使用GCC 4.7.2.
我正在尝试使用可变参数模板来指定朋友类。我尝试使用以下语法,但是不起作用。
template <class... Args>
struct A {
friend Args...;
};
Run Code Online (Sandbox Code Playgroud)
我尝试编写一些变通办法,但是由于友谊不是可传递的和继承的,因此似乎并不是那么简单。因此,问题是是否存在正确的语法或任何变通方法,以使Args中的每个单独的类成为A的朋友?
我刚从朋友处得到一个问题.
#include<stdio.h>
void fun(int[][3]);
int main(void){
int a[3][3]={1,2,3,4,5,6,7,8,9};
fun(a);
printf("\n%u",a);
a++;//Ques 1
printf("\n%u",a);
printf("%d",a[2][1]-a[1][2]);
return 0;
}
void fun(int a[][3]){
++a;//Ques 2
a[1][1]++;
}
Run Code Online (Sandbox Code Playgroud)
第1行将抛出L值的错误,因为'a'是二维数组的名称.但是,对于第2行的情况,情况并没有发生.
谁能明白这个疑问?
我有一个复杂的程序,std::array<double, N>它使用N的小值.它用于operator[]从这些数组中获取值.
我发现GCC 6.1有-O2或-O3没有内联这些调用,导致这些C++数组比它们的C等价物慢.
这是生成的程序集:
340 <std::array<double, 8ul>::operator[](unsigned long) const>:
340: 48 8d 04 f7 lea (%rdi,%rsi,8),%rax
344: c3 retq
345: 90 nop
346: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
34d: 00 00 00
Run Code Online (Sandbox Code Playgroud)
为每个大小的数组发出相同的代码(因为没有边界检查).
这样一个数组的循环如下所示:
4c0: e8 7b fe ff ff callq 340 <std::array<double, 8ul>::operator[](unsigned long) const>
4c5: be 07 00 00 00 mov $0x7,%esi
4ca: 4c 89 f7 mov %r14,%rdi
4cd: 48 89 44 24 78 mov …Run Code Online (Sandbox Code Playgroud) 从上一个问题的接受答案中,我发现了一条我不知道的关于模板和格式良好的规则
如果出现以下情况,则程序格式错误,无需诊断:
- [...]
- 可变参数模板的每个有效特化都需要一个空模板参数包,或者
- [...]
根据这条规则(如果我理解正确的话),以下模板函数的格式不正确
template <typename ... Ts>
int foo (std::tuple<Ts...> const &)
{ return std::get<sizeof...(Ts)>(std::tuple<int>{42}); }
Run Code Online (Sandbox Code Playgroud)
因为唯一有效的专业化需要和空Ts...参数包。
但是(也许是因为我不太懂英语)在模板具有两个以上参数包的情况下,我不确定是否理解这条规则。
我的意思是......以下foo()功能
#include <tuple>
#include <iostream>
template <typename ... Ts, typename ... Us>
int foo (std::tuple<Ts...> const &, std::tuple<Us...> const &)
{ return std::get<sizeof...(Ts)+sizeof...(Us)-1U>(std::tuple<int>{42}); }
int main ()
{
auto t0 = std::tuple<>{};
auto t1 = std::tuple<int>{0};
//std::cout << foo(t0, t0) << std::endl; // compilation error
std::cout << foo(t0, t1) << std::endl; // print …Run Code Online (Sandbox Code Playgroud) 有明显一些Linux专柜perf一样syscall:sys_enter_select,但我的系统上perf list不显示任何人
其他人确实有这些计数器的证据在这里:http : //www.brendangregg.com/blog/2014-07-03/perf-counting.html
如果我运行perf top -e 'syscalls:sys_enter_*'它说:
Can't open event dir: Permission denied
invalid or unsupported event: 'syscalls:sys_enter_*'
Run Code Online (Sandbox Code Playgroud)
其他事件类型( 中的perf list)工作正常。
我需要做什么来访问系统调用计数器perf?我在 x86_64 上使用 Linux 内核和 perf 版本 3.10。
c++ ×7
c++11 ×5
boost ×2
templates ×2
boost-python ×1
c ×1
enable-if ×1
exception ×1
for-loop ×1
friend ×1
gcc ×1
inline ×1
linux ×1
linux-kernel ×1
lvalue ×1
optimization ×1
overloading ×1
pep ×1
perf ×1
python ×1
python-3.x ×1
refactoring ×1
shared-ptr ×1
well-formed ×1