小编Que*_*tin的帖子

测试是否可以使用元编程调用f(x)

Stroustrup的书提供了一个如何回答这个问题的例子:" f(x)如果x是类型的话,是否可以调用X"(第28.4.4节"使用Enable_if的更多示例").我试图重现这个例子,但是出了点问题,无法理解.

在我的下面的代码中,有一个函数f(int).我希望那时的结果has_f<int>::value1(true).实际结果是0(false).

#include <type_traits>
#include <iostream>

//
// Meta if/then/else specialization
//
struct substitution_failure { };

template<typename T>
struct substitution_succeeded : std::true_type { };

template<>
struct substitution_succeeded<substitution_failure> : std::false_type { };

//
// sfinae to derive the specialization
//
template<typename T>
struct get_f_result {
private:
  template<typename X>
    static auto check(X const& x) -> decltype(f(x));
  static substitution_failure check(...);
public:
  using type = decltype(check(std::declval<T>())); …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming c++11

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

std :: variant可以定义多少种类型?

我已经知道在c ++ 17中有一个std :: variant类型.看起来变体容器不支持预定义的数据类型,但是对于每种变体类型,用户可以定义自己的数据类型集.

std::variant<int, float> v;
Run Code Online (Sandbox Code Playgroud)

我想知道,类型列表可以多长时间?库是否具有用于Aleksandrescu方式的最大参数数量的预定义模板,或者是编译器支持的变体,并且类型的数量不受限制?

c++ variant c++17

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

提升精神X3:"属性没有预期的大小",但它为什么关心?

我正在尝试用Spirit X3编写一个解析器,但我还没有走得太远,因为我遇到了一个我无法弄清楚的编译错误.我想我知道编译器在抱怨什么,但我不明白为什么它会关心.以下代码编译(clang 3.9,Boost 1.62.0)并且有效.(我意识到它的结构很差,并且有常量的内置解析器;我只是试图证明这个问题.)

#define BOOST_SPIRIT_X3_DEBUG

#include <iostream>
#include <boost/spirit/home/x3.hpp>

using namespace std;
namespace x3 = boost::spirit::x3;

namespace lang {

    using namespace x3;

    struct Constant {
    };

    rule<class constant_id, Constant> const constant = "constant";

    auto const integer_literal = -char_('-') >> +digit;
    auto const float_literal = -char_('-') >> +digit >> -(char_('.') >> *digit);

    auto const constant_def = (integer_literal)[([](auto &ctx) { _val(ctx) = Constant(); })];

    BOOST_SPIRIT_DEFINE(constant);
}

int main(int argc, char *argv[]) {

    string s("3.14159");
    if (x3::phrase_parse(s.begin(), s.end(), lang::constant, x3::space)) {
        cout …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit boost-spirit-x3

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

在向量中引用对象(Modern C++)

如果这很简单,我很抱歉,但我已经玩了超过15年的C++.考虑这个简单的例子:向量包含类型的对象A.类BA对象必须引用驻留在向量中的对象.(编辑说明 - 类B必须有一个引用该A实例的成员)

回到那一天你会宣布一个A*并完成它,但今天如何使用智能指针做到这一点?我不想在向量中存储共享或唯一指针,因为我不希望在堆上分配A对象.它们必须在矢量本身.

c++ smart-pointers c++11

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

当元素数&gt; 1000时,如何使向量的笛卡尔积?

我有1,2,...,n个向量。每个向量都有超过10000个元素,我必须获得这些向量的笛卡尔积。我有一个代码,起作用了,但是仅在1000个元素和4个向量以下。我想将笛卡尔乘积写到文件中,但是如果输出文件大于1GB,则会得到:“在抛出'std :: bad_alloc'what()实例what():std :: bad_alloc实例后调用终止”。

我的主要问题是,如何解决此内存分配错误?

这是我的代码的可运行部分:

#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <fstream>
#include <math.h>

using namespace std;

vector<double> makeVectorByRange(double min, double max, double step){
    vector<double> out = {};
    for( ; min <= max; min+=step){
        out.push_back(min);
    }
    return out;
} 


void cart_product_solve_and_write_to_file (const vector<vector<double>>& inpV) {
    vector<vector<double>> out = {{}};

    std::ofstream outputFile;
    std::fixed;

    for (auto& u : inpV) {
        vector<vector<double>> r;
        r.clear();
        for (auto& x : out) {

            //make/open file, append
            outputFile.open ("out.csv", std::ofstream::out | std::ofstream::app); …
Run Code Online (Sandbox Code Playgroud)

c++ cartesian-product bad-alloc c++11

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

有没有办法将一对元组剥离为可变参数模板类型或使用可变参数类型实例化某些内容?

我有一个tupleof pairs,其中每对都有一个固定类型和一个变体类型。我想要的是一个变体类型列表或用这些类型定义一个对象(它肯定是用可变参数模板包构造的)。

根据How do I strip a tuple<>back into a variadic template list of types? ,可以用 的构成类型实例化某些东西tuple。但在这种情况下,我不知道这是否可能,如果可能,我不知道该怎么做。

想象一下我有这样的事情:

struct MyFixedType
{ /* ... */ };


using Tuple = std::tuple<
    std::pair<MyFixedType, int>,
    std::pair<MyFixedType, double>,
    std::pair<MyFixedType, std::string>
>;
Run Code Online (Sandbox Code Playgroud)

我有这门课:

template <typename... Ts>
class MyClass
{
};
Run Code Online (Sandbox Code Playgroud)

我要的是申报对象MyClass<int, double, std::string>

注意:虽然这种情况可以使用预处理器或一些宏来处理,但我不允许使用宏。

c++ c++11

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

在这个例子中,std :: variant如何成为valueless_by_exception?

这个例子的灵感来自cppreference的例子

struct S {
    operator int() { throw 42; }
};


int main(){
    variant<float, int> v{12.f}; // OK
    cout << std::boolalpha << v.valueless_by_exception() << "\n";
    try{
        v.emplace<1>(S()); // v may be valueless
    }
    catch(...){
    }
    cout << std::boolalpha << v.valueless_by_exception() << "\n";   
}
Run Code Online (Sandbox Code Playgroud)

对于一个编译器,我尝试了它输出

虚假,真实

意味着emplace导致变体变得毫无价值

我不明白的是这是怎么回事.特别是我不明白为什么emplace被调用,我希望程序甚至不会调用它,因为从S转换为int参数抛出.

c++ exception variant c++17

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

如何声明恒定的方法指针

1 ST关断这不是关于如何指向一个恒定方法问题。我想知道如何使我的方法指针恒定。

鉴于:

struct foo {
    void func1();
    void func2();
};
Run Code Online (Sandbox Code Playgroud)

我可以使用构建方法指针,void (foo::*bar)() = &foo::func1 但是以后可以使用,bar = &foo.func2我想避免这种情况。

我可以轻松地做到这一点const auto bar = &foo::func1,但是我不确定在之前如何做到这一点。

c++ member-function-pointers function-pointers constants c++03

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

用 std 实现替换 boost

替换它的正确方法是什么:

std::ostringstream buf;
std::for_each(bd.begin(), bd.end(), buf << boost::lambda::constant("&nbsp;") << boost::lambda::_1);
Run Code Online (Sandbox Code Playgroud)

使用不使用 boost 的实现?这是我尝试过的:

std::string backspace("&nbps;");
std::ostringstream buf;        
std::for_each(bd.begin(), bd.end(), buf << backspace << std::placeholders::_1);
Run Code Online (Sandbox Code Playgroud)

第二个 '<<' 用红色下划线表示,我收到错误消息:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Ph<1>' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

c++ boost std c++11

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

`git log --follow --graph`跳过提交

建立

git version 2.11.0.windows.1
Run Code Online (Sandbox Code Playgroud)

这是一个重现我的测试存储库的bash片段:

git init

# Create a file
echo Hello > a.txt
git add a.txt
git commit -m 'First commit'

# Change it on one branch
git checkout -b feature
echo Hi > a.txt
git commit -am 'Change'

# Rename it on the other
git checkout master
git mv a.txt b.txt
git commit -m 'Move'

# Merge both changes
git merge --no-edit feature
Run Code Online (Sandbox Code Playgroud)

最后,git log --graph --pretty=oneline --abbrev-commit打印:

*   06b5bb7 Merge branch 'feature'
|\
| …
Run Code Online (Sandbox Code Playgroud)

git git-log

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