小编All*_*lan的帖子

为什么我不能在OutEdgeList = listS和VertexList = listS中使用boost图形write_graphviz

为什么我不能编译以下简单的应用程序.如果我将listS更改为vecS,那么每件事情都可以.(我使用的是boost 1.46.1和gcc 4.4.5)

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

int main(int argc, const char *argv[]) {
    boost::adjacency_list< boost::listS, boost::listS, boost::bidirectionalS > g;

    boost::write_graphviz(std::cout, g);

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

boost boost-graph

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

使用 std::ofstream 时如何设置 O_CLOEXEC

什么是 C++ (std::ofstream) 等价物:

int fd = open(fn,O_WRONLY|O_NDELAY|O_APPEND|O_CREAT|O_CLOEXEC,0600);
Run Code Online (Sandbox Code Playgroud)

我想使用它的应用程序只能在较新版本的 linux 上运行,因此可移植性不是问题。

c++ linux

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

IOCTL 调用中指向缓冲区的指针

我正在使用 Ruby 和 bit-struct 在测试设置中配置我的网络接口。这对于大多数 IOCTL 调用都适用,但我不知道如何调用 SIOCGIFCONF

这是一个例子:

如果我想获取接口的 MAC 地址,我会写这样的内容:

class LinuxIfreqMacAddr < BitStruct
  char       :name,    128
  unsigned   :type,     16, :endian => :native
  hex_octets :macaddr,  48
  pad        :padding,  64
end

ifr = LinuxIfreqMacAddr.new
ifr.name = "eth0"
s.ioctl(SIOCGIFHWADDR, ifr) # s is a socket
puts ifr.macaddr
Run Code Online (Sandbox Code Playgroud)

这工作正常,并且会打印 eth0 的 MAC 地址。但是“struct ifconf”的签名(与 SIOCGIFCONF 一起使用)需要传递一个缓冲区。

这是签名:

struct ifconf  {
    int     ifc_len;
    char __user *ifcu_buf;
};
Run Code Online (Sandbox Code Playgroud)

如何从 ruby​​ 调用具有 4096 字节缓冲区的 SIOCGIFCONF ioctl 命令?

ruby

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

提升shared_from_this和析构函数

我发现不允许在类的析构函数中调用shared_from_this:

https://svn.boost.org/trac/boost/ticket/147

此行为是设计使然。由于析构函数会销毁该对象,因此为它创建一个shared_ptr是不安全的,因为一旦析构函数结束,它就会变得悬空。

我理解这个论点,但是如果我需要一个“shared_from_this”指针来清理引用(而不是共享所有权)怎么办?

这是一个我没有使用shared_ptr的例子:

class A{
public:
    A( Manager * m ) : m_(m) {
        m_->add(this);
    }

    ~A() {
        m_->remove(this);
    }

private:
    Manager * m_;
};
Run Code Online (Sandbox Code Playgroud)

这里我尝试将其翻译为共享指针。但我找不到完成析构函数的好方法:

class A : public boost::enable_shared_from_this< A > {
public:
    typedef boost::shared_ptr< A > Ptr;

    static Ptr create( Manager * m ) {
        Ptr p( new A(m));
        p->init();
        return p;
    }

    ~A() {
        // NON-WORKING
        // m_->remove( shared_from_this() );
    }

private:
    A( Manager * m ) : m_(m) { }

    void init() { …
Run Code Online (Sandbox Code Playgroud)

c++ boost shared-ptr object-lifetime weak-ptr

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

可变参数模板模板参数

以下代码不使用clang 3.0进行编译,这是因为我做错了吗?因为在c ++ 11中不允许它,或者因为clang不支持它?

template<int OFFSET>
struct A {
    enum O { offset = OFFSET };
};

template < template <int T> class Head, typename... Tail>
struct C : public Head<1>, private C<Tail> { };

int main()
{
    C< A, A > c1;

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

编译错误:

test3.cxx:99:42: error: template argument for template template parameter must be a class template or type alias template
    struct C : public Head<1>, private C<Tail> { };
                                         ^
test3.cxx:103:15: error: use of class template …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11

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

简化的元组实现

我正在开发一个没有任何标准库的平台,我正在尝试实现一个"类似元组"的类型.

我只需要设备来获取类型,但我希望编译器发出一个static_assert如果类型不存在的话.没有必要在重复类型上声明,但它会很好......

这是我尝试过的:

template<class... Args> struct SimpleTuple;

template<> struct SimpleTuple<> {
    SimpleTuple() { }
};

template<class Head, class... Tail>
struct SimpleTuple<Head, Tail...> {
    typedef Head    HeadType;
    typedef SimpleTuple<Tail...> VATailType;

    SimpleTuple(Head head, Tail... tail) : data(head), rest(tail...) { }

    // Does not compile, because explicit specialization in non-namespace
    // scope is not allowed
    template<typename T> T& get() {
        return rest.get<T>();
    }

    template<> Head& get() {
        return data;
    }

    Head data;
    VATailType rest;
};
Run Code Online (Sandbox Code Playgroud)

我知道它失败的原因,但我不知道如何避免这种限制.

我正在寻找一个尽可能简单的实现.

c++ c++11

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

模板化成员函数的地址

在下面的例子中,我如何找到成员函数 f 的地址

template<typename HANDLER>
void serialize(HANDLER &h) {
    //  Compiler error (gcc 4.8.1)
    //  test.cxx: In function ‘void serialize(HANDLER&)’:
    //  test.cxx:9:26: error: expected primary-expression before ‘int’
    //       auto x = &HANDLER::f<int>;
    //                            ^
    //  test.cxx:9:26: error: expected ‘,’ or ‘;’ before ‘int’
    auto x = &HANDLER::f<int>;
}

struct HandlerA {
    template<typename T> void f() { }
};

struct HandlerB {
    template<typename T> void f() { }
};

struct HandlerC {
    template<typename T> void f() { }
};

int main() …
Run Code Online (Sandbox Code Playgroud)

templates c++11

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

在函数重载中使用标记

为什么X中的方法测试不明确,如何解决?

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> {};

int main(int argc, const char *argv[])
{
    X x;
    x.test(A());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

In function ‘int main(int, const char**)’:
error: request for member ‘test’ is ambiguous
error: candidates are: void I<T>::test(T) [with T = B]
error:                 void I<T>::test(T) [with T = A]
Run Code Online (Sandbox Code Playgroud)

c++ generic-programming

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

在"模板化基类"中调用模板方法时出错

以下代码无法编译,为什么会这样?我该如何解决这个问题?

struct A{
    template<int N> int get() { return N; }
};

template <typename X>
struct B : public X {
    template<int N> int get() {
        return X::get<N>();
    }
};

int main(int argc, const char *argv[])
{
    B<A> b;
    return b.get<5>();
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

test.cxx: In member function ‘int B<X>::get()’:
test.cxx:8:30: error: expected primary-expression before ‘)’ token
test.cxx: In member function ‘int B<X>::get() [with int N = 5, X = A]’:
test.cxx:15:25:   instantiated from here
test.cxx:8:30: error: invalid operands of types …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates generic-programming

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

在vim中的当前缓冲区上自定义语法高亮显示

我不时会在当前缓冲区上应用一些自定义的额外语法高亮显示.

如何使用vim语法/高亮系统中的构建来完成(我不想使用Highlight插件)

例如,我想强调当前缓冲区中的所有断言语句.

vim vim-syntax-highlighting

3
推荐指数
2
解决办法
728
查看次数