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&合法的
我目前遇到的问题是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)
唯一重新定义的函数ChildModel是Model::getLinkCost,并且该Model::getLinkCost方法仅通过方法调用Model.所有方法都在C++文件中定义Model.cpp.
关于缓冲区溢出,以下哪一项是安全的?
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减去一个),但问题非常微妙,而且我已经看到了代码建议.有志于引用标准的志愿者吗?
假设我有一个赋值为'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'类型?
所以我回答了这个问题:定义类模板的友元函数模板,我从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) 我有一个使用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) 我有一个围绕向量和形状的包装结构,如下所示:
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})明确指定排名的地方?
以下代码在 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 …当我写命令时,choco install 'Name'发生了这样的事情:
未安装“名称”。未找到列出的来源的软件包。来源:'https://chocolatey.org/api/v2/' 注意:当您指定显式来源时,它会覆盖默认来源。如果软件包版本是预发行版本并且您没有指定
--pre,则可能找不到该软件包。请参阅https://chocolatey.org/docs/troubleshooting获取更多帮助。
使用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++ ×7
c++14 ×2
auto ×1
c ×1
c++17 ×1
c++20 ×1
chocolatey ×1
fmt ×1
friend ×1
g++ ×1
hash ×1
input ×1
installation ×1
long-integer ×1
numpy ×1
polymorphism ×1
python ×1
scanf ×1
templates ×1