小编Moc*_*han的帖子

“struct std::enable_if<false, void> 中没有名为“type”的类型

为什么这不起作用?

它给了我一个错误

error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
Run Code Online (Sandbox Code Playgroud)

这我不把getX()getY()里面struct B它的工作原理。但是,如果我将它们都放在一个结构中,则不会。

error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
Run Code Online (Sandbox Code Playgroud)

但是,这很好用。

#include <iostream>
#include <boost/type_traits/has_dereference.hpp>

struct A {
    A(int i) : x(i) {}
    int x;
};

template<typename T>
struct B {
    template<typename std::enable_if_t<!boost::has_dereference<T>::value> * = nullptr>
    auto getX(T t) {
        return t.x;
    }

    template<typename std::enable_if_t<boost::has_dereference<T>::value> * = nullptr>
    auto getX(T …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits

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

gcc 预编译头文件:主文件中的 pragma once

我创建了一个头文件。一些简单的如下。

#pragma once

#include <iostream>

template<typename T>
void say(T t) {
    std::cout << t << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

然后用于g++创建gch带有g++ hello.h. 它给了我这个警告->

pch.h:2:9: warning: #pragma once in main file
    2 | #pragma once
      |         ^~~~
Run Code Online (Sandbox Code Playgroud)

但是gch创建的文件和预编译的头文件工作正常。如果我使用标题守卫,这个错误就会消失。

我在这里做错了吗?

c++ g++ pragma precompiled-headers

9
推荐指数
1
解决办法
2994
查看次数

CMake 使用与 CMakeLists.txt 不同的文件

有没有办法向 CMake 指定我想使用与 CMakeLists.txt 不同的文件?我似乎找不到任何东西。

我面临的问题是我不允许签入对 CMakeLists.txt 所做的更改。我也经常切换分支。

我想为我的开发修改一些构建首选项。如果我编辑 CMakeLists.txt,git 将不允许我切换分支。

如果我有 cmake 可以使用的 CMyMakeLists.txt,那么它就可以解决这个问题。有没有办法做到这一点?

cmake

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

C++ 编译时检查模板类型中是否存在方法

我有一个调用成员函数的模板。我如何检查static_assert该方法是否存在?

struct A {

};

struct B {
    int foo() { return 42; } };

template <typename T> struct D {
    static_assert(/* T has foo */, "T needs foo for reasons");

    int bar() {
       return t.foo();
    }

    T t; };

int main() {
    D<A> d;

    std::cout << d.bar() << std::endl;

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

我知道这只会产生 A 没有 foo 的编译器错误,但我想检查并使用static_assert.

c++ templates

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

获取任何C ++可取消引用类型的基础类型

我有一个函数可以创建的基础类型的新对象P。这P是一种可取消引用的类型,如指针或智能指针。

template<typename P>
auto make_new()
Run Code Online (Sandbox Code Playgroud)

例如,对于指针和智能指针,

struct A
{
    int a = 3;
};

A* a = make_new<A*>();
std::cout << a->a << std::endl;
delete a;

std::shared_ptr<A> b = make_new<std::shared_ptr<A>>();
std::cout << b->a << std::endl;
Run Code Online (Sandbox Code Playgroud)

现在,对于共享指针,我将实现make_new以下内容,

template<typename P>
auto make_new()
{
    using Ptype = typename P::element_type;
    return P(new Ptype);
}
Run Code Online (Sandbox Code Playgroud)

这不适用于指针。

现在,既适用于指针又适用于智能指针的东西,

template<typename P>
auto make_new()
{
    using Ptype = typename std::remove_reference<decltype(*P())>::type;
    return P(new Ptype);
}
Run Code Online (Sandbox Code Playgroud)

但不适用于std::optional

有没有一种规范的方法来获取可取消引用对象的基础类型?

我知道,*并且->可以重载到任何东西,并且不能保证构造函数可以像上面那样工作,或者说有意义。 …

c++ templates pointers

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

std :: functions和lambda函数传递

我有一个类std::function作为参数,我分配一个lambda函数.它在构造函数中工作,但之后停止工作.f运行第一行后调试器显示为"空".为什么?

#include <iostream>
#include <string>
#include <functional>

typedef std::function<void(std::string)> const& fn;

class TestClass
{
public:
    TestClass(fn _f) : f(_f) { F(); }
    void F() { f("hello"); };

private:
    fn f;
};


int main()
{
    TestClass t([](std::string str) {std::cout << str << std::endl; });

    t.F();

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

呼叫t.F()导致故障.为什么?

我可以通过将其更改为以下内容来解决此问题:

int main()
{
    fn __f = [](std::string str) {std::cout << str << std::endl; };
    TestClass t(__f);

    t.F();

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

但同样,这并不当我改变工作fnauto! …

c++ lambda c++11 std-function

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

c ++:if块语法内部有多个分配,用于短路

我希望能够在if块中进行多个分配,如果第一个失败则将其短路。但是,这不会编译并表示,expected primary-expression before ‘auto’

#include <iostream>
#include <optional>

std::optional<int> foo()
{
    return 0;
}

int main() {
    if (auto a = foo() && auto b = foo())
    {
        std::cout << "a = " << *a << ", b = " << *b << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的作品,虽然和我想要的。

    if (auto a = foo())
    {
      if (auto b = foo()) {
        std::cout << "a = " << *a << ", b = " << *b << std::endl;
      }
    }
Run Code Online (Sandbox Code Playgroud)

但是我有办法在第一个语法中使用语法吗?使用括号将表达式括起来不起作用。

c++ short-circuiting

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

Boost Graph Library - 有向图的最小生成树

我有一个问题,要求我在 Boost Graph Library 中找到有向图的最小生成树。

我的第一次尝试是使用深度优先搜索和 DFS 访问者。我的计划是忽略除树边回调之外的所有边。这是行不通的,我用下面的例子来说明原因。

我的问题是我是否可以让我的 dfs-visitor 在 BGL 中创建有向图的最小生成树。

它有一些算法,并且已经在这里讨论过(Finding a minispanning tree on a有向图),我无法判断它是否已针对 BGL 实现,或者它只是对 BGL 中已有内容的简单修改。

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/depth_first_search.hpp>


struct my_visitor : public boost::dfs_visitor<>
{
    template <class Edge, class Graph>
    void back_edge(Edge e, const Graph&) 
    {
        std::cout << "Back edge: " << e << std::endl;
    }

    template <class Edge, class Graph>
    void forward_or_cross_edge(Edge u, const Graph& g)
    {
        std::cout << "Forward or cross edge: " << …
Run Code Online (Sandbox Code Playgroud)

algorithm tree boost graph boost-graph

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

flat_map虽然仍然借用,但仍然放在这里

我正在尝试编写一个带有字符串索引和字符串中每个字符的向量.

在下面的示例中,0 J,0 a,0 n ... 1 J,1i,...

fn main() {
    let names = vec!["Jane", "Jill", "Jack", "Johne"];
    let name3 = names.iter().enumerate().flat_map(|(i, name)| {
        let letters = name.chars();
        let mut is = Vec::new();
        for _k in 0..name.len() {
            is.push(i);
        }
        is.iter().zip(letters).collect::<Vec<_>>()
    });
    for i in name3 {
        println!("{:?}", i);
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我错误

error[E0597]: `is` does not live long enough
  --> src/main.rs:9:9
   |
9  |         is.iter().zip(letters).collect::<Vec<_>>()
   |         ^^ borrowed value does not live long enough
10 |     });
   |     - `is` …
Run Code Online (Sandbox Code Playgroud)

dictionary flat rust

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

什么是do_cos_slow.isra?

我编写了一个简单的代码进行测试prof

double bar_compute (double d) {
    double t = std::abs(d);
    t += std::sqrt(d);
    t += std::cos(d);

    return t;
}

// Do some computation n times
double foo_compute(unsigned n) {
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<double> dist(0.0, 1.0);

    double total = 0;
    for (int i=0; i<n; i++) {
        double d = dist(mt);
        total += bar_compute(d);
    }
    return total;
}
Run Code Online (Sandbox Code Playgroud)

当我运行prof并查看输出时,它是

  56.14%  runcode  libm-2.23.so       [.] __cos_avx                                                                                                                                
  27.34%  runcode  runcode            [.] _Z11foo_computej                                                                                                                         
  13.92%  runcode  runcode            [.] _Z11bar_computed                                                                                                                         
   0.86%  runcode  libm-2.23.so …
Run Code Online (Sandbox Code Playgroud)

trigonometry glibc avx perf

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

具有结构和继承的初始化列表

我正在为结构使用初始值设定项列表。但是,它不适用于继承。

这段代码很好。

struct K {
int x, y;
};

K k {1, 2};
Run Code Online (Sandbox Code Playgroud)

但是,这会产生错误。

struct L : public K {};
L l {1, 2};
Run Code Online (Sandbox Code Playgroud)

此外,这不起作用。

struct X {
    int x;
};

struct Y : public X {
    int y;
};

Y y {1, 2};
Run Code Online (Sandbox Code Playgroud)

有没有办法使用具有继承结构的初始化列表。我在模板中使用它们,因此无论它是否是继承类,它都不会编译。

c++ inheritance struct initialization initializer-list

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

enable_if和自动返回类型?

我想使用type_traits通过shared_ptr进行过载。

struct A {
    A(int i) : x(i) {}
    int x;
};

int main()
{
    A a{4};
    auto b = std::make_shared<A>(7);
    A& c = a;
    A* d = b.get();
    A* e = &a;

    std::cout << getX(a) << std::endl;
    std::cout << getX(b) << std::endl;
    std::cout << getX(c) << std::endl;
    std::cout << getX(d) << std::endl;
    std::cout << getX(e) << std::endl;

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

这是一种解决方案,但是存在必须预先定义返回类型的问题。

template <typename T>
typename std::enable_if_t<!boost::has_dereference<T>::value, int> getX(T t)
{
    return t.x;
}

template <typename T>
typename std::enable_if_t<boost::has_dereference<T>::value, int> …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits enable-if

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