小编Hol*_*olt的帖子

为什么不同的类型变量可以用作C++中const引用参数的参数的参数

void foo(const int& v) {
    int x = v;
    std::cout << x;
}

int main()
{
    unsigned y = 1;
    foo(y);
}
Run Code Online (Sandbox Code Playgroud)

是用C++ y代替const int&合法的

c++ pass-by-reference

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

g ++警告,使用但未定义的内联虚函数

我目前遇到的问题是警告我无法摆脱.我的代码工作正常,但此警告不断弹出:

ChildModel.h:136:24:警告:内联函数virtual int ChildModel :: getLinkCost(const Link&)const使用但从未定义[默认启用]

我目前在SO上发现了这个帖子,同样的问题,但答案是特定于库(定义的东西)所以它对我不起作用.

我的代码如下:

class Model {
public:
    virtual inline int getLinkCost(Link const& link) const;
};

class ChildModel: public Model {
public:
    /** Warning on the line bellow: **/
    virtual inline int getLinkCost(Link const& link) const;
};
Run Code Online (Sandbox Code Playgroud)

唯一重新定义的函数ChildModelModel::getLinkCost,并且该Model::getLinkCost方法仅通过方法调用Model.所有方法都在C++文件中定义Model.cpp.

c++ g++

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

scanf字段宽度字符串溢出

关于缓冲区溢出,以下哪一项是安全的?

char buf[10] = {0};
scanf("%10s", buf);
Run Code Online (Sandbox Code Playgroud)

要么

char buf[10] = {0};
scanf("%9s", buf);
Run Code Online (Sandbox Code Playgroud)

从我读过的内容来看,我要去的是第二个(sizeof减去一个),但问题非常微妙,而且我已经看到了代码建议.有志于引用标准的志愿者吗?

c input scanf

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

为什么不能numpy计算长对象?

假设我有一个赋值为'long'的变量

x = 40*2*10**30
Run Code Online (Sandbox Code Playgroud)

如果我然后尝试使用numpy(导入为np)获取此变量的日志:

np.log10(x)
Run Code Online (Sandbox Code Playgroud)

我遇到属性错误:

'long'对象没有属性'log10'.

为了解决这个问题,我可以将变量设置为float并且它可以正常工作或使用'math'包:

math.log10(x)
np.log10(float(x))
Run Code Online (Sandbox Code Playgroud)

我的问题是:math.log10和np.log10有何不同,为什么没有设置np来处理'long'类型?

python numpy long-integer

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

返回类型匹配auto和friend功能

所以我回答了这个问题:定义类模板的友元函数模板,我从g ++(5.3)和clang(3.8)中发现了一些"怪异"的行为:

我们假设以下模板:

template<int M>
struct test {
private:
    int value;

    template<int U, int K>
    friend test<K> foo (test<U> const t);
};

template <int M, int N = 2 * M>
test<N> foo (test<M> const t) {
    test<N> r;
    r.value = t.value;
    return r;
}

int main(){
    test<1> t;
    foo(t);
}
Run Code Online (Sandbox Code Playgroud)

这与两个编译器一起编译(如预期的那样 - 如果这不应该编译,请随意发表评论并解释原因).

如果我改变了:

template<int U, int K>
friend auto foo(test<U> const t);

template <int M, int N = 2 * M>
auto foo (test<M> const t) …
Run Code Online (Sandbox Code Playgroud)

c++ friend language-lawyer auto c++14

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

散列多态类型的正确方法

我有一个使用Howard Hinnant的方法(基于hash_append重载的泛型哈希)实现的哈希过程.

该方法的目的是创建类的哈希以"记住"计算结果(参见本答案的结尾),所以我面临一些问题.特别是,请考虑以下可能Input需要哈希的类:

struct A {
    virtual int do_stuff() const = 0;
    virtual ~A(); 
};
struct B: A {
    int do_stuff() const override { return 0; }
};
struct C: A {
    const int u;
    int do_stuff() const override { return u; }
};

struct Input {
    A const& a; // store a reference to an instance of B or C
};
Run Code Online (Sandbox Code Playgroud)

现在,如果我想哈希Input,我会有类似的东西:

template <class HashAlgorithm>
void hash_append(HashAlgorithm& h, Input const& input) …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism hash c++14

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

如何制作接受任何等级数组的函数或构造函数

我有一个围绕向量和形状的包装结构,如下所示:

template <std::size_t N>
struct array_type {
    std::array<std::size_t, N> shape;
    std::vector<float> data;
};
Run Code Online (Sandbox Code Playgroud)

我想能够构造一个array_type从任何阵列float[N]float[N][M]等等。

目前,我对每个等级都有一个函数,例如

template <std::size_t N>
struct array_type {
    std::array<std::size_t, N> shape;
    std::vector<float> data;
};
Run Code Online (Sandbox Code Playgroud)

我想写一些类似的东西:

template <class Array>
array_type<std::rank_v<Array>> make_array(Array const&);
Run Code Online (Sandbox Code Playgroud)

...但这不适用于初始化列表:

auto arr1 = _1d({1, 2, 3}); // Ok
auto arr2 = make_array({1, 2, 3}); // Ko
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点make_array?或者(因为我认为这是不可能的),至少有类似make_array<1>({1, 2, 3})明确指定排名的地方?

c++ c++17

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

使用额外的不可推导模板参数重载函数是否有效?

以下代码在 gcc (9)、clang (11) 和 msvc (16.28) 下编译并正常工作:

template <class A>
struct X {
    A a;

    constexpr X(A a) : a{a} { }
};

template <class A>
constexpr auto fn(X<A> const& x) {
    return X<A>(x.a - 1);
}


template <class Xs, class A>
constexpr auto fn(X<A> const& x) {
    return Xs(x.a - 1);
}

constexpr X<int> x1{3};
constexpr auto x2 = fn(x1);
constexpr auto x3 = fn<X<double>>(x1);
Run Code Online (Sandbox Code Playgroud)

除了第二个fn函数中的额外Xs参数外,有两个函数具有相同的声明。

我想确定这是标准接受的,而不是这些编译器提供的额外内容?由于所有 3 个都这样做,我想这将是标准的,但你永远不知道。

我还想知道我对为什么这项工作/将成为标准的假设是否正确:

  • 在通话中fn(x1)Xs …

c++ templates language-lawyer

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

我无法使用 Chocolatey 安装软件包

当我写命令时,choco install 'Name'发生了这样的事情:

未安装“名称”。未找到列出的来源的软件包。来源:'https://chocolatey.org/api/v2/' 注意:当您指定显式来源时,它会覆盖默认来源。如果软件包版本是预发行版本并且您没有指定--pre,则可能找不到该软件包。请参阅https://chocolatey.org/docs/troubleshooting获取更多帮助。

installation chocolatey

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

将 std::format 与具有运算符 &lt;&lt; 的类型一起使用

使用fmt库,您可以轻松格式化已定义的类型operator<<。正如这里所解释的,您可以ostream_formatter使用一行代码进行扩展:

template <> struct fmt::formatter<Foo> : ostream_formatter {};
Run Code Online (Sandbox Code Playgroud)

现在你可以这样做:

fmt::format("Foo is {}", Foo());
Run Code Online (Sandbox Code Playgroud)

类似的事情可能吗std::format()?我有已经定义的类型operator<<,所以我想开始将它们与std::format().

如果我尝试编写自己的格式化程序,我不知道如何ostream从成员函数中的参数中获取 an format()

template <> struct fmt::formatter<Foo> : ostream_formatter {};
Run Code Online (Sandbox Code Playgroud)

直接编写格式化程序而不依赖于 是否更好operator<<

c++ c++20 fmt

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