小编lur*_*her的帖子

validate integer是一些枚举类项(C++ 11)

我有一些枚举课

enum class Foo { A=1, B=18 , Z=42 };
Run Code Online (Sandbox Code Playgroud)

我想检查是否可以将某个整数转换为a Foo.这样做的理想方式是什么?这是用于运行时检查(在编译时尚不知道整数)

显然我可以用这种方式做到这一点(编写一个bool CheckEnum(Foo);带有大屁股开关的函数,除了默认情况下,所有情况都返回true),但我希望有一个更优雅的机制,避免这么多的写作.MPL或Boost.Preprocessor将是一个完全可以接受的解决方案,但其中一个我遗憾地知道很少

c++ c++11 enum-class

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

r值引用返回类型语义?

像这样的返回类型是否代表c ++ 11中有意义的东西?

template <typename R>
R&& grabStuff();

T instance = grabStuff<T>();
Run Code Online (Sandbox Code Playgroud)

grabStuff如果R没有移动构造函数,我希望应该抛出编译时错误,因为这似乎不允许返回类型使用复制构造函数

c++ rvalue-reference c++11

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

用于检测模板特化的模板元函数

这个问题的启发,我想知道是否有一些编译时检查可以引入检测两个给定的模板实例:

template <typename T>
class Templ...

typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
Run Code Online (Sandbox Code Playgroud)

是从相同的定义构造的,或者如果它们是从Templ模板的不同特化构建的

所以基本上假设的模板函数将表现如下:

template <typename T>
class Templ
{}

template <>
class Templ<std::string>
{}

template <>
class Templ<double>
{}

template <typename T1,typename T2>
class Belong_To_Same_Templ_Definition
{}

//tests
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
typedef Templ<int> intInstance;
typedef Templ<char> charInstance;

assert( Belong_To_Same_Templ_Definition< intInstance , charInstance >::value == true);
assert( Belong_To_Same_Templ_Definition< intInstance , doubleInstance >::value == false);
assert( Belong_To_Same_Templ_Definition< stringInstance , doubleInstance >::value == false);
Run Code Online (Sandbox Code Playgroud)

有可能创造这种元功能吗?

c++ static-assert compile-time template-specialization template-meta-programming

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

std :: unique_ptr :: reset和构造函数异常

如果初始化unique_ptr如下:

std::unique_ptr<Foo> i;
i.reset( new Foo() ); 
Run Code Online (Sandbox Code Playgroud)

但抛出异常Foo::Foo(),问题是:内存分配会发生什么?unique_ptr如何避免泄露?这是在new运营商内部处理的吗?

当范围退出时,肯定会调用析构函数.由于在返回reset之前不调用调用new Foo(),所以new当异常离开构造函数时,似乎必须通过释放分配的内存来处理它.

这是怎么回事?

c++ constructor memory-management exception-handling

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

将模块写入.bc bitcode文件

我假设从一个模块转储一个.bc文件是一个微不足道的操作,但现在,我第一次必须从代码中实际执行它,对于我的生活,我在这个过程中找不到一个缺失的步骤:

static void WriteModule ( const Module *  M, BitstreamWriter &  Stream )
Run Code Online (Sandbox Code Playgroud)

http://llvm.org/docs/doxygen/html/BitcodeWriter_8cpp.html#a828cec7a8fed9d232556420efef7ae89

要编写该模块,首先我需要一个BistreamWriter

BitstreamWriter::BitstreamWriter (SmallVectorImpl< char > &O)
Run Code Online (Sandbox Code Playgroud)

http://llvm.org/docs/doxygen/html/classllvm_1_1BitstreamWriter.html

对于BitstreamWriter,我需要一个SmallVectorImpl.但是,接下来呢?我应该自己在文件处理程序上逐字节写下SmallVectorImpl的内容吗?这有一个llvm api吗?我需要别的吗?

file-io llvm

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

从LLVM位代码创建位置无关的目标文件

我有一个llvm模块,我已经作为bitcode文件转储llvm::WriteBitcodeToFile.我想将此bitcode文件转换为包含模块中的函数的本机动态可加载库.

我该怎么做呢?我尝试使用llc它,但这产生的代码显然不可重定位,因为在执行以下步骤后:

llc -enable-pie -cppgen=functions -filetype=asm executableModule -o em.s
Run Code Online (Sandbox Code Playgroud)

然后,用gnu​​汇编as到一个目标文件中:

as -o mylib.o em.s
Run Code Online (Sandbox Code Playgroud)

最后,尝试生成一个共享库:

gcc -shared -o libmyfile.so -fPIC mylib.o
Run Code Online (Sandbox Code Playgroud)

失败并出现错误:

/usr/bin/ld: error: mylib.o: requires dynamic R_X86_64_PC32 reloc against 'X.foo' which may overflow at runtime; recompile with -fPIC
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

linux llvm dynamic-linking

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

git文件历史浏览

有没有办法在git仓库中浏览历史版本(而不是简单的差异)?我看起来像你想要的tortoiseSvn log/diff浏览器,但对于ubuntu.我不想直接看到差异,而是在原始上下文中看到整个文件的版本,突出显示以前版本的差异

git ubuntu-10.04

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

MockPP和Google Mock的利弊

以下模拟框架如何相互比较?与Java对应物相比,它们的主要限制是什么?

c++ mocking googlemock

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

移动构造函数和初始化列表

我想为某个类型实现移动构造函数(没有复制构造函数),该类型需要是一个值类型boost::unordered_map.我们称之为这种类型Composite.

Composite 有以下签名:

struct Base
{
  Base(..stuff, no default ctor) : initialization list {}
  Base(Base&& other) : initialization list {} 
}

struct Composite
{
  Base member;
  Composite(..stuff, no default ctor) : member(...) {}
  Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure this invokes the move ctor of Base
}
Run Code Online (Sandbox Code Playgroud)

我想写这个,所以boost::unordered_map< Key , Composite >不需要复制构造函数,只需使用移动构造函数.如果可能的话,我不想Base在移动构造函数的初始化列表中使用复制构造函数Composite.

这可能吗?

c++ unordered-map initialization move-semantics c++11

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

未来的可组合性,以及boost :: wait_for_all

我刚刚阅读了文章' Futures Done Right ',c ++ 11承诺的主要缺点似乎是从现有的创造复合期货

我现在正在查看boost :: wait_for_any的文档

但请考虑以下示例:

int calculate_the_answer_to_life_the_universe_and_everything()
{
    return 42;
}

int calculate_the_answer_to_death_and_anything_in_between()
{
    return 121;
}

boost::packaged_task<int> pt(calculate_the_answer_to_life_the_universe_and_everything);
boost:: future<int> fi=pt.get_future();
boost::packaged_task<int> pt2(calculate_the_answer_to_death_and_anything_in_between);
boost:: future<int> fi2=pt2.get_future();

....


int calculate_the_oscillation_of_barzoom(boost::future<int>& a, boost::future<int>& b)
{
    boost::wait_for_all(a,b);
    return a.get() + b.get();
}

boost::packaged_task<int> pt_composite(boost::bind(calculate_the_oscillation_of_barzoom, fi , fi2));
boost:: future<int> fi_composite=pt_composite.get_future();
Run Code Online (Sandbox Code Playgroud)

这种可组合性方法有什么问题?这是实现可组合性的有效方法吗?我们需要一些优雅的语法功能吗?

c++ multithreading boost future c++11

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