小编seh*_*ehe的帖子

MSVC12认为从std :: array派生的聚合不是pod

鉴于以下内容

#include <array>

struct litmus final : std::array<unsigned char, 16>
{
};

static_assert(std::is_pod<std::array<unsigned char, 16> >::value, "not pod");

// this fails on MSVC:
static_assert(std::is_pod<litmus>::value, "not pod");
Run Code Online (Sandbox Code Playgroud)

以下编译器同意litmuspod:

但是,MSVC12(VS2013 RTM)认为第二个断言失败.

  1. 谁是对的?
  2. 是否有任何技巧可以让MSVC将该类视为pod?

编辑有关信息:is_trivially_copyable<litmus>在MSVC上返回true-ness.对于许多不严格要求实际POD的情况,这可能很有用.

c++ static-assert pod visual-c++

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

如何强类型定义非基本类型?

请注意以下程序,其中函数接受期望类型和作为该类型的typedef的任何类型.

//a user defined type
class Widget{};

//a function that takes a Widget
void function (Widget w){}

int main(){

    //make a typedef (this is C++11 syntax for a typedef. It's the same thing)
    using Gadget = Widget;

    //make the two "different types" (well.. they're not really different as you will see)
    Widget w;
    Gadget g;

    //call a function that should ONLY accept Widgets
    function(w); //works (good)
    function(g); //<- works (I do not want this to compile though)

}
Run Code Online (Sandbox Code Playgroud)

如您所见,typedef实际上并不区分新类型.我想改为从类型继承:

//inherit …
Run Code Online (Sandbox Code Playgroud)

c++ boost types typedef c++11

8
推荐指数
2
解决办法
729
查看次数

pthread_key_create析构函数没有被调用

根据pthread_key_create手册页,我们可以关联在线程关闭时调用的析构函数.我的问题是我注册的析构函数没有被调用.我的代码的要点如下.

static pthread_key_t key;
static pthread_once_t tls_init_flag = PTHREAD_ONCE_INIT;

void destructor(void *t) {
  // thread local data structure clean up code here, which is not getting called
}

void create_key() {
  pthread_key_create(&key, destructor);
}

// This will be called from every thread
void set_thread_specific() {

  ts = new ts_stack; // Thread local data structure

  pthread_once(&tls_init_flag, create_key);
  pthread_setspecific(key, ts);
}
Run Code Online (Sandbox Code Playgroud)

知道什么可能会阻止这个析构函数被调用?我现在也在使用atexit()来在主线程中进行一些清理.有没有机会干扰被调用的析构函数?我也尝试删除它.虽然仍然没有奏效.另外我不清楚我是否应该将主线程作为atexit的单独案例来处理.(顺便说一下,使用atexit是必须的,因为我需要在应用程序出口处进行一些特定于应用程序的清理)

c++ shutdown pthreads thread-specific-storage

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

通过弹出最后一个类型来创建一个新的元组类

我尝试了以下代码,但它给出了:

main.cpp:29:22:错误:聚合'pop<std::tuple<int, char, float> > p'类型不完整,无法定义

我错过了什么?

template <typename T>
struct pop;

template <typename E, typename... Ts>
struct pop<tuple<Ts..., E>> {
    using result = tuple<Ts...>;
};

tuple<int, char, float> t;
typename pop<decltype(t)>::result p;
Run Code Online (Sandbox Code Playgroud)

如果Ts ...必须在类型列表的末尾,为什么它在http://en.cppreference.com/w/cpp/language/parameter_pack中的示例中有效:

template<class A, class B, class...C> void func(A arg1, B arg2, C...arg3)
{
    container<A,B,C...> t1;  // expands to container<A,B,E1,E2,E3> 
    container<C...,A,B> t2;  // expands to container<E1,E2,E3,A,B> 
    container<A,C...,B> t3;  // expands to container<A,E1,E2,E3,B> 
}
Run Code Online (Sandbox Code Playgroud)

variadic-templates c++11

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

获取文件夹中的有序文件列表

我用boost::filesystem::directory_iterator它来获取给定文件夹中所有可用文件的列表.

问题是我认为这种方法会按字母顺序给我文件,而结果似乎很随机.

是否有按字母顺序排序的奇特方式?

我目前的代码:

if(boost::filesystem::is_directory(myFolder)){
    // Iterate existing files
    boost::filesystem::directory_iterator end_iter;
    for(boost::filesystem::directory_iterator dir_itr(myFolder);
        dir_itr!=end_iter; dir_itr++){

        boost::filesystem::path filePath;
        // Check if it is a file
        if(boost::filesystem::is_regular_file(dir_itr->status())){
            std::cout << "Reading file " << dir_itr->path().string() << std::cout;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-filesystem

8
推荐指数
2
解决办法
7880
查看次数

boost图形库 - 顶点颜色和graphviz输出的最小示例

作为对boost图库的新手,我发现通常很难弄清楚哪些示例与特定示例相关联以及哪些部分对于使用是通用的.

作为练习,我正在尝试制作一个简单的图形,为顶点指定颜色属性,并将结果输出到graphviz,因此颜色显示为渲染的颜色属性.任何帮助,将不胜感激!这是我到目前为止(更具体的使用问题在这里的评论):

#include "fstream"
#include "boost/graph/graphviz.hpp"
#include "boost/graph/adjacency_list.hpp"

struct vertex_info { 
    int color; 
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, vertex_info> Graph;
typedef std::pair<int, int> Edge;

int main(void) {
  Graph g;
  add_edge(0, 1, g);
  add_edge(1, 2, g);

  // replace this with some traversing and assigning of colors to the 3 vertices  ...
  // should I use bundled properties for this?
  // it's unclear how I would get write_graphviz to recognize a bundled property as the color attribute
  g[0].color = 1; 

  std::ofstream outf("min.gv"); …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-graph

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

计算字符串中的单词 - c编程

我需要编写一个函数来计算字符串中的单词.出于此赋值的目的,"单词"被定义为非空的非空白字符序列,通过空格与其他单词分隔.

这是我到目前为止:

int words(const char sentence[ ]);

int i, length=0, count=0, last=0;
length= strlen(sentence);

for (i=0, i<length, i++)
 if (sentence[i] != ' ')
     if (last=0)
        count++;
     else
        last=1;
 else
     last=0;

return count;
Run Code Online (Sandbox Code Playgroud)

我不确定它是否有效,因为我无法测试它直到我的整个程序完成并且我不确定它是否会起作用,是否有更好的方法来编写这个函数?

c string function

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

在Spirit中构建自定义表达式树:Qi(没有Utree或Boost :: Variant)

首先,如果使用Boost Variant或Utree更容易,那么我将与他们达成和解,我将尝试在另一个主题中解决我们的问题.但是,我非常希望能够像下面一样构建一棵树.

背景,如果你想直接讨论这个问题,请忽略:我希望能够构建一个解析像

"({a} == 0) && ({b} > 5)"
Run Code Online (Sandbox Code Playgroud)

或标准的数学表达式

"(2 * a) + b"
Run Code Online (Sandbox Code Playgroud)

然后我将在评估树之前定义a和b是什么,如下所示:

a = 10;
double val = myExpression->Evaluate();
Run Code Online (Sandbox Code Playgroud)

我的问题来自于当我尝试构建try以将字符串解析为我的表达式树时.我正在使用一个抽象类"Expression",然后导出"变量","常量"和"二进制"表达式(它也会做一元,但它不应该影响我的问题.我一直有使用我的规则添加到树的问题所以我显然做错了什么.我很难绕着属性缠头.

我的树如下(Tree.h):

class BinaryExpression;
typedef double (*func)(double, double);

class Expression
{
public:
    virtual double Evaluate() = 0;
};

class BinaryExpression : public Expression
{
private:
    Expression* lhs;
    Expression* rhs;
    func method;

    double Evaluate();

public:
    BinaryExpression(void);
    BinaryExpression(char op, Expression* lhs, Expression* rhs);
    BinaryExpression(char op);
    void operator()(Expression* lhs, Expression* rhs);
};

class ConstantExpression : public Expression
{
private: …
Run Code Online (Sandbox Code Playgroud)

c++ parsing expression-trees boost-spirit boost-spirit-qi

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

将包含字符串成员的类调整为合成属性

我正在尝试将字符串解析为symbol包含std::string成员的自定义类型的属性.我以为我可以BOOST_FUSION_ADAPT_STRUCT在这里使用,但这不起作用.

如果我宣布规则是rule<It, std::string(), space_type>有效的.如果我将其定义为rule<It, symbol(), space_type>失败,并显示错误"no type name value_typein symbol".我认为Spirit正在尝试将值字符附加到属性中,该属性会按预期失败.但是,如果没有添加额外的中间规则来捕获std::string属性,是不是有办法使这项工作成功?

这是完整的MWE:

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>

struct symbol 
{ 
    std::string repr; 
};

BOOST_FUSION_ADAPT_STRUCT(symbol, (std::string, repr))

namespace qi = boost::spirit::qi;

template <typename Iterator>
struct test_grammar : qi::grammar<Iterator, symbol(), qi::ascii::space_type> {
    test_grammar() : test_grammar::base_type{start} {
        start = qi::lexeme[+qi::char_("a-z")];
    }

    qi::rule<Iterator, symbol(), qi::ascii::space_type> start;
};

#include <iostream>

auto main() -> int {
    test_grammar<std::string::iterator> grammar{};
    auto input …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit boost-fusion

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

使用表达式树调用不安全的方法

我需要调用unsafe带有原始指针的方法.

为此,我需要构造Expression表示指向由VariableExpression或表示的值的指针ParameterExpression.

怎么做?

c# reflection unsafe expression-trees

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