小编Pra*_*ian的帖子

为蒙特卡罗模拟种子mt19937_64的最佳方法

我正在研究一个运行蒙特卡罗模拟的程序; 具体来说,我正在使用Metropolis算法.该程序需要产生数十亿的"随机"数字.我知道梅森捻线机在蒙特卡罗模拟中非常受欢迎,但我想确保以最佳方式播种发电机.

目前我正在使用以下方法计算32位种子:

mt19937_64 prng; //pseudo random number generator
unsigned long seed; //store seed so that every run can follow the same sequence
unsigned char seed_count; //to help keep seeds from repeating because of temporal proximity

unsigned long genSeed() {
    return (  static_cast<unsigned long>(time(NULL))      << 16 )
         | ( (static_cast<unsigned long>(clock()) & 0xFF) << 8  )
         | ( (static_cast<unsigned long>(seed_count++) & 0xFF) );
}

//...

seed = genSeed();
prng.seed(seed);
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,有更好的方法来确保不重复的新种子,我很确定mt19937_64可以播种超过32位.有没有人有什么建议?

c++ random mersenne-twister c++11 random-seed

30
推荐指数
2
解决办法
6268
查看次数

C++成员函数与自由函数

我在编写程序时很多时候一直对这个设计决策感到困惑,但是我不能100%确定何时应该将函数作为类的成员函数,何时将其保留为正常函数,其中其他源文件可以在头文件中公开函数声明时调用该函数.在大多数情况下,对类的成员变量的期望访问是否与决策有关?

c++ methods function

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

如何让std :: make_unique成为我班级的朋友

我想声明std::make_unique函数是我班级的朋友.原因是我想声明我的构造函数protected并提供一种使用创建对象的替代方法unique_ptr.这是一个示例代码:

#include <memory>

template <typename T>
class A
{
public:
    // Somehow I want to declare make_unique as a friend
    friend std::unique_ptr<A<T>> std::make_unique<A<T>>();


    static std::unique_ptr<A> CreateA(T x)
    {
        //return std::unique_ptr<A>(new A(x)); // works
        return std::make_unique<A>(x);         // doesn't work
    }

protected:
    A(T x) { (void)x; }
};

int main()
{
    std::unique_ptr<A<int>> a = A<int>::CreateA(5);
    (void)a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我收到此错误:

Start
In file included from prog.cc:1:
/usr/local/libcxx-head/include/c++/v1/memory:3152:32: error: calling a protected constructor of class 'A<int>'
return …
Run Code Online (Sandbox Code Playgroud)

c++ templates unique-ptr friend-function c++14

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

Boost Log 2.0:日志中的空严重级别

我正在使用Boost-Log 2.0,它与版本1存在一些差异,我很难输出"Severity"属性.

我正在使用"Boost.Format-style"格式化程序

"%TimeStamp% [%Uptime%] (%LineID%) <%Severity%>: %Message%"
Run Code Online (Sandbox Code Playgroud)

TimeStamp,LineIDMessagecommon_attributes.Uptime是我添加的属性attrs::timer().我认为Severity在使用时会自动添加severity_logger,但显然不是,这是我的问题.我得到空的严重性,例如:

2013-Apr-06 19:21:52.408974 [00:00:00.001337] (3) <>: A warning severity message
Run Code Online (Sandbox Code Playgroud)

注意空<>.我试图添加严重性,register_simple_formatter_factory但后来我得到编译器错误:

error: no matching function for call to ‘register_simple_formatter_factory(const char [9])’
Run Code Online (Sandbox Code Playgroud)

而且我不明白为什么.

这是我的代码:

#include <iostream>


#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>

#include <boost/log/attributes.hpp> …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-log

25
推荐指数
2
解决办法
1万
查看次数

移动构造函数 - 默认构造函数VS 2013的无效类型

我正在阅读有关移动构造函数的内容,我在VS 2013中执行了此代码...

class Student
{
    unique_ptr<string> pName_;

public:
    Student(string name) : pName_(new string(name)) { }
    ~Student() { }
    Student(Student&&) = default;  // Here I get the error.
    void printStudentName(void) { cout << *pName_ << endl; }
};

int main(void)
{
    vector<Student> persons;

    Student p = Student("Nishith");
    persons.push_back(std::move(p));
    persons.front().printStudentName();

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

Student::Student(Student&& )当我尝试编译时,我得到" :不是可以默认的特殊成员函数"...

任何人都可以解释我为什么会收到此错误?

c++ c++11 visual-studio-2013

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

配置Flycheck以使用C++ 11

我在为C++ 11配置flycheck时遇到了很大的麻烦.现在,flycheck正在抨击像std::to_string().我使用的检查器只是g ++.我可以在.emacs文件中添加什么,以便flycheck默认采用C++ 11?

c++ emacs c++11 flycheck

25
推荐指数
2
解决办法
1万
查看次数

明确删除shared_ptr

这里有一个简单的问题:您是否可以明确删除boost::shared_ptr自己?你应该吗?

澄清,我不是说删除指针所持的指针shared_ptr.我的意思是实际的shared_ptr.我知道大多数人建议不要这样做,所以我只是想知道是否可以明确地做到这一点.

c++ boost shared-ptr boost-smart-ptr

23
推荐指数
3
解决办法
5万
查看次数

对象传递给std :: move但是没有从中移动?

我正在审查一些像这样的代码,其中A是一个可移动的类型:

// Returns true exactly when ownership of a is taken
bool MaybeConsume(A&& a) {
  if (some condition) {
    Consume(std::move(a));  // ???
    return true;
  }
  return false;
}

// ... elsewhere ...

A a;
if (!MaybeConsume(std::move(a))) {
  a.DoSomething();  // !!!
}
Run Code Online (Sandbox Code Playgroud)

我们的静态分析工具抱怨a在移动(at !!!)后使用.IIUC std::move只是一个static_cast,并且在调用a移动构造函数或赋值运算符(可能是在Consume)之前,该对象实际上不会被删除.假设MaybeConsume在评论中满足合同,

  1. 这有用吗?
  2. 是UB吗?
  3. std::move???无操作?

(可能这个特殊的实例可以重构以避免微妙,但我仍然想要求我自己的理解).

c++ undefined-behavior rvalue-reference c++11

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

递归尾随返回类型的名称解析

我在显式和自动尾随返回类型之间发现了一个奇怪的区别.

在下面的代码中,我们定义了一个在整数和iter函数上模板化的结构,它将这种类型的一个对象作为参数.返回类型取决于在递减模板值后调用自身的结果.

为了打破实例化循环(或者我认为),我提供了一个返回非依赖类型的特化.

我们有一个玩具主体来实例化模板.

这是一些代码:

template<int i> struct Int {};

constexpr auto iter(Int<0>) -> Int<0>;

template<int i> constexpr auto iter(Int<i>) -> decltype(iter(Int<i-1>{}));

int main(){
  decltype(iter(Int<10>{})) a;
}
Run Code Online (Sandbox Code Playgroud)

此代码在gcc 4.9和clang 3.5中都不起作用.两者都触发无限实例化(它们与专用基本情况不匹配).

rec.cpp:11:62: fatal error: recursive template instantiation exceeded maximum depth of 256
template<int i> constexpr auto iter(Int<i>) -> decltype(iter(Int<i-1>{}));
Run Code Online (Sandbox Code Playgroud)

现在,如果我们使用C++ 14 decltype(auto)并且我们为模板提供了一个返回完全相同的东西:

template<int i> struct Int {};

constexpr auto iter(Int<0>) -> Int<0>;

template<int i>
constexpr auto iter(Int<i>) -> decltype(auto) {
  return iter(Int<i-1>{});
}

int main(){
  decltype(iter(Int<10>{})) a;
}
Run Code Online (Sandbox Code Playgroud)

这现在适用于两个编译器并按预期运行.

我尝试了不同的方式来表达专业化并稍微移动一下(要小心它的位置),但这并没有阻止它的自焚;( …

c++ c++11 c++14

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

对于const的引用,std :: is_const的等价物是什么?

考虑一下代码:

int const  x = 50;
int const& y = x;
cout << std::is_const<decltype(x)>::value << endl; // 1
cout << std::is_const<decltype(y)>::value << endl; // 0
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为y不是const参考,它是对a的引用const.

有没有foo这样std::foo<decltype(y)>::value的1?如果没有,那么定义我自己的是什么样的呢?

c++ types const

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