小编sam*_*ise的帖子

使用简单类型列表实现的指数编译时间。为什么?

我正在尝试使用 C++ 类型列表。下面是一个类型列表过滤器函数的简单实现。除了 gcc 和 clang 中的编译时间超出 18 个元素之外,它似乎还可以工作。我想知道我可以做哪些改进来使这个实用。

#include <type_traits>

// a type list
template <class... T> struct tl ;

// helper filter for type list 
template <class IN_TL, class OUT_TL, template <typename> class P>
struct filter_tl_impl;

// Base case
template <class... Ts, template <typename> class P>
// If the input list is empty we are done
struct filter_tl_impl<tl<>, tl<Ts...>, P> {
  using type = tl<Ts...>;
};

// Normal case
template <class Head, class... Tail, class... Ts2, template <typename> …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming variadic-templates

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

gcc生成具有执行权限的共享对象

我正在使用gcc在linux上构建一个共享库.我没有编译或链接错误,但我的共享对象总是设置执行位(尽管readelf指示它是一个共享对象).我只是这样做:

> echo "int f() {return 1;}" > a.cpp
> gcc -c a.cpp
> gcc -shared -o liba.so a.o
> ls -l liba.so
-rwxr-xr-x 1 me me 6652 2011-06-09 17:05 liba.so
Run Code Online (Sandbox Code Playgroud)

为什么共享对象会设置执行位?

linux gcc shared

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

我的C++片段中无法解释的断言失败

以下是我能够从更大的代码库中提取的片段,希望能够说明目前我无法看到的某种内存损坏.这是在Ubuntu 17.04上使用g ++ 6.3.0,虽然我在gcc 7.0.1和clang 4.0.0上看到了同样的问题.

#include <array>                                                                                                                                                                                                
#include <assert.h>                                                                                                                                                                                             

using Msg = std::array<char,sizeof(std::string)*2> ;                                                                                                                                                            

class Str {                                                                                                                                                                                                     
public:                                                                                                                                                                                                         
   explicit Str (std::string &&v) : v (std::move(v)) {}                                                                                                                                                         
   std::string v;                                                                                                                                                                                               
};                                                                                                                                                                                                              

void f(Msg &tmsg)                                                                                                                                                                                               
{                                                                                                                                                                                                               
   Msg m;                                                                                                                                                                                                       
   new (&m) Str ("hello");                                                                                                                                                                                      
   tmsg = m;                                                                                                                                                                                                    
}                                                                                                                                                                                                               

int main( int , char* [] )                                                                                                                                                                                      
{                                                                                                                                                                                                               
   Msg tmsg;                                                                                                                                                                                                    
   f(tmsg);                                                                                                                                                                                                     
   auto ptr = (Str*) &tmsg;                                                                                                                                                                                     
   assert(ptr->v == "hello");    // This fails                                                                                                                                                                               
   return 0;                                                                                                                                                                                                    
}                                                                                                                                                                                                               
Run Code Online (Sandbox Code Playgroud)

当我尝试运行时,我得到:

$ g++ main.cpp -g -std=c++11 && ./a.out
a.out: main.cpp:24: int …
Run Code Online (Sandbox Code Playgroud)

c++ memory-corruption

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