小编Ale*_*der的帖子

为什么我们不能在静态成员函数中使用const成员?

class TConst
{
    const int i;
    int& ref;
    public:
    TConst(int n):i(n),ref(n){}
    static void p1(){prn(i);}//error here
};
Run Code Online (Sandbox Code Playgroud)

当我尝试conststatic成员函数中使用类成员时,我的编译器会生成错误.

为什么不允许?

c++ static-methods const

9
推荐指数
3
解决办法
1138
查看次数

将const引用返回给本地对象时到底发生了什么?

struct A {
    A(int) : i(new int(783)) {
        std::cout << "a ctor" << std::endl;
    }

    A(const A& other) : i(new int(*(other.i))) {
        std::cout << "a copy ctor" << std::endl;
    }

    ~A() {
        std::cout << "a dtor" << std::endl;
        delete i;
    }

    void get() {
        std::cout << *i << std::endl;
    }

private:
    int* i;
};

const A& foo() {
    return A(32);
}

const A& foo_2() {
    return 6;
}

int main()
{
    A a = foo();
    a.get();
}
Run Code Online (Sandbox Code Playgroud)

我知道,返回对本地值的引用是不好的.但是,另一方面,const引用应该延长临时对象的生命周期.

此代码生成UB输出.所以没有生命延伸.

为什么?我的意思是有人可以解释一下子发生什么事吗? …

c++ const-reference

7
推荐指数
2
解决办法
3654
查看次数

使用Alternative解析器提升精神表现不佳

我已经问过这个问题.但由于没有答案,我现在再次询问完整的可编辑源代码片段.

由于boost :: variant move semantic存在一些问题,因此应该使用no std = c ++ 11选项编译此代码段.只是'g ++ -Wall -pedantic'.

在此代码段中,您将找到"// Comment here"行.您可以在下面的句子中发表评论,直到"//这里-----".如果取消注释此块,则此程序的性能非常差.

所以只要我能看到瓶颈就是替代解析器.我需要的是一些关于改进/改变我的语法以提高解析性能的建议.谢谢.

码:

#include <string>
#include <vector>
#include <iostream>

#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/variant.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/array.hpp>
#include <boost/variant/apply_visitor.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

namespace client {
  typedef std::string::iterator input_iterator_type;

  struct op_and {}; 
  struct op_or …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit boost-spirit-qi

6
推荐指数
1
解决办法
503
查看次数

boost shared_ptr operator =

这是代码示例

class A{
  int i;
public:
  A(int i) : i(i) {}
  void f() { prn(i); }
};

int main()
{
  A* pi = new A(9);
  A* pi2= new A(87);
  boost::shared_ptr<A> spi(pi);
  boost::shared_ptr<A> spi2(pi2);
  spi=spi2;
  spi->f();
  spi2->f();
  pi->f();
  pi2->f();
}
Run Code Online (Sandbox Code Playgroud)

输出:

87
87
0
87
Run Code Online (Sandbox Code Playgroud)

问题是为什么输出中为0?

文档说明:效果:等效于shared_ptr(r).swap(*this).

但是如果shared_ptr对象刚刚交换,结果应该是9.如果第一个对象被删除,应该有Segmentation fault.

为什么0?

c++ boost shared-ptr

5
推荐指数
2
解决办法
1107
查看次数

qt 垃圾收集和智能指针

我想开始在我的 qt 工作中使用智能指针。让我困惑的是智能指针如何与 Qt 垃圾收集一起使用。整个 Qt 都建立在子 QObject 以 QObject* parent 作为 ctor 参数构造的习惯用法上,因此启用了垃圾收集。例如:

     QWidget* mWidget = new QWidget(this);//Here we not only 
                                          //ensure that mWidget will be deleted  
                                          //when its parent is deleted, but also tell qt,  
                                          //that mWidget is not a window, but belongs to 
                                          //parent's layout         
Run Code Online (Sandbox Code Playgroud)

现在,如果我想将 mWidget 包装到智能指针中。

 typedef QScopedPointer<QWidget> WidgPtr;
 WidgPtr mWidget = WidgPtr(new QWidget(this));
Run Code Online (Sandbox Code Playgroud)

但是现在当父的 dtor 被调用时,它会在 mWidget 的指针上调用 delete 两次。首先是由于垃圾收集,其次是调用智能指针 dtor 时。

当然,我们可以在没有父级的情况下构造 mWidget,然后更改一些标志以关闭窗口行为或调用 setParent()(但是再次 mWidget 将被删除两次)。但是对我来说,为了能够使用智能指针而不是原始指针而进行如此复杂的初始化太过分了。或者也许我错过了什么?谢谢。

c++ qt smart-pointers

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

为什么SFINAE在这里不起作用

我想实现静态检查以找出类型是否可构造:

#include <iostream>

template<typename T>
struct IsConstr
{
    typedef char TrueType;
    typedef struct{char a[2];} FalseType;
    template<typename C>
    static decltype((C(),TrueType())) test(int);
    template<typename C>
    static FalseType test(...);

    enum{value=(sizeof(test<T>(0))==sizeof(TrueType))};
}; 


struct S{private: S();};
int main()
{
    std::cout<<IsConstr<S>::value<<std::endl;
    std::cout<<IsConstr<int>::value<<std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码无法编译,显示错误

"替换失败.'S :: S()'是私人的".

为什么SFINAE不在这里工作?谢谢.

c++ sfinae

5
推荐指数
0
解决办法
170
查看次数

为什么stringstream :: str()截断字符串?

我有stringstream对象.它是通过填补

stringstream ss;
boost::iostreams::copy(inp,ss);
Run Code Online (Sandbox Code Playgroud)

boost::iostreams::filtering_streambuf<boost::iostreams::input> inp;
Run Code Online (Sandbox Code Playgroud)

并实际上保持在其中的ungzipped文件.

现在,如果我将stringstream内容刷新到文件

std::ofstream ofs(path_to_file,std::ios_base::out|std::ios_base::binary);
ofs << ss.rdbuf();
Run Code Online (Sandbox Code Playgroud)

一切都好.文件中包含完整的正确数据.

但是,如果我而不是像这样刷新文件构造字符串

std::string s = ss.str();
Run Code Online (Sandbox Code Playgroud)

内容在中间某处被截断.它不是持久性错误,它显然取决于字符串缓冲区的内容.

内容是几种语言的HTML文件.

它能是什么?谢谢.

c++ string stringstream boost-iostreams

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

带参数和不带参数的构造函数之间的区别

这是两个班级

class A
{
    std::string s;
public:
    A() { prn("A constructor"); }
    A(std::string s) : s(s) { prn("A not empty constructor"); }
    A(const A&) { prn("A copy constructor"); }
    A& operator =(const A& a) { prn("A = operator"); return *this; }
    void p() { prn("in A"); }
};

class B
{
public:
    A a;
    B(A aa) : a(aa) { prn("B constructor"); }
    B() { prn("B default constructor"); }
};
Run Code Online (Sandbox Code Playgroud)

现在以下代码正常工作

B b(A("sa"));
b.a.p();
Run Code Online (Sandbox Code Playgroud)

打印:

一个不空的构造
拷贝构造函数
乙构造
的一个

但是如果我使用没有参数的A构造函数会发生奇怪的事情

B …
Run Code Online (Sandbox Code Playgroud)

c++ constructor

0
推荐指数
1
解决办法
1499
查看次数

编译打印时出错任何一个值

我正在尝试编译简单的代码片段.

main = (putStrLn . show) (Right 3.423)
Run Code Online (Sandbox Code Playgroud)

编译导致以下错误:

No instance for (Show a0) arising from a use of `show'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
  instance Show Double -- Defined in `GHC.Float'
  instance Show Float -- Defined in `GHC.Float'
  instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
    -- Defined in `GHC.Real'
  ...plus 42 others
In the second argument of `(.)', namely …
Run Code Online (Sandbox Code Playgroud)

haskell either

0
推荐指数
1
解决办法
77
查看次数