当我尝试
apt-get install -y tzdata
Run Code Online (Sandbox Code Playgroud)
显示选择时区的命令行选项.我试图在脚本中使用它来进行一些设置,如何在没有用户输入的情况下运行apt-get?
我知道要重新配置我能做的tzdata
echo "America/New_York" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
Run Code Online (Sandbox Code Playgroud)
但是在安装时我需要它完全运行,即使它没有设置正确的时区,我总是可以重新配置它.
我试过了
echo 5 | apt-get install -y tzdata
Run Code Online (Sandbox Code Playgroud)
但它没有按预期工作.
为什么这段代码有效?同c++14
// Example program
#include <iostream>
#include <string>
using namespace std;
auto fun()
{
struct a
{
int num = 10;
a()
{
cout << "a made\n";
}
~a()
{
cout << "a destroyed\n";
}
};
static a a_obj;
return a_obj;
}
int main()
{
auto x = fun();
cout << x.num << endl;
}
Run Code Online (Sandbox Code Playgroud)
主要的类型如何a?如果我auto x=改为a x=它显然不编译,但主要知道该类型a?
该static声明是有,因为我是想测试别的东西,但后来我偶然发现了这种行为.
在试图解决一个问题,我开始思考这个-给予user-defined class和2 comparators吧,让我们说我们有2套std::set<user_class,comparator_inc>,并std::set<user_class,comparator_dec>在comparators排序上的一个值增减值user_class(一个简单的int也许).这是我的代码:
#include <iostream>
#include <set>
using std::cout;
using std::endl;
using std::set;
struct A
{
int val;
};
struct c_inc
{
bool operator()(const A& first,const A& second) const
{
return first.val > second.val;
}
};
struct c_dec
{
bool operator()(const A& first,const A& second) const
{
return first.val < second.val;
}
};
int main()
{
set<A,c_inc> s1;
set<A,c_dec> s2;
auto x = s1.insert({1});
cout << x.first->val …Run Code Online (Sandbox Code Playgroud) 在这段代码中
#include <type_traits>
#include <iostream>
template <class T>
void func(const T& a)
{
if constexpr(std::is_same_v<T,int>)
{
static_cast<void>(a);
}
else if constexpr(std::is_same_v<T,double>)
{
// oops forgot to use it here
}
else
{
}
}
int main() {
func(4);
func("this");
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不警告 中未使用的变量else-s()?(和-Wall)
我的理解是,逻辑上该方法的实例化是完全不同的。如果该参数未在模板的实例化之一中使用,那么它不是一个未使用的变量吗?或者语言/编译器不会这样解释它。
我试图了解lru_cache decoratorpython (3) 中的实现,特别是它如何为其内部创建keyfrom 函数。argsdict
我一直在读这个https://github.com/python/cpython/blob/master/Lib/functools.py#L414
我无法理解为什么kwd_mark = (object(),)函数中有这个。我看到它只tuple用一个实例来创建 a object(),看起来它就像函数调用的所有 theargs和kwargs/kwdsthe之间的分隔符。key我想了解它是否在这里做了什么特别的事情https://repl.it/repls/ExpectiveFinishedSandboxes但我想不出任何东西。
#include <iostream>
using namespace std;
template <typename T>
class test {
public:
T value;
template <typename... Args, typename = decltype(T())>
test(Args... args): value(args...)
{
cout <<"ctor running\n";
}
template <typename... Args>
test(Args...) : value(1)
{
cout <<"ctor unspec running\n";
}
};
class t
{
public:
t() = delete;
explicit t(int) {}
};
int main()
{
test<t> h;
}
Run Code Online (Sandbox Code Playgroud)
我试图为constructor创建的对象(h)调用第二个.我不知道为什么会收到此错误:
Run Code Online (Sandbox Code Playgroud)prog.cc: In function 'int main()': prog.cc:45:13: error: call of overloaded 'test()' is ambiguous test<t> h; ^ prog.cc:25:5: …
我试图调用stored procedure从python使用sqlalchemy。我的代码如下:
@database_connection_wrapper
def get_adv(connection):
''' get the adv using a stored proc from the database '''
connection.callproc("GetAdvMedianTrailing30Days")
results = list(connection.fetchall())
print(results)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
AttributeError: 'pyodbc.Connection' object has no attribute 'callproc'
我按照官方文档上的说明进行操作,但这并没有什么区别。
我在 python 3.5
有不同的调用方式stored procedures吗?
首先,我道歉,如果这是重复,我会很乐意把它删除,但我甚至不确定这里的问题/诊断是什么.
无论如何,我的代码在这里使用gcc而不是clang - 为什么会这样?我显然无法理解为什么clang不能编译它.
#include <iostream>
#include <type_traits>
using std::cout;
using std::endl;
template <typename T, typename... Args, typename std::enable_if<!sizeof...(Args)>::type* = nullptr>
void func(int start_size, int idx)
{
cout << start_size << " " << idx << endl;
return;
}
template <typename T, typename... Args, typename std::enable_if<sizeof...(Args)>::type* = nullptr>
void func(int start_size, int idx)
{
if((idx + 1) == int(start_size - int(sizeof...(Args))))
{
cout << start_size << " " << idx << endl;
return;
}
func<Args...>(start_size, idx);
}
template <typename... Args> …Run Code Online (Sandbox Code Playgroud) 我在这里看https://github.com/abseil/abseil-cpp/blob/master/absl/types/span.h#L673
template <int&... ExplicitArgumentBarrier, typename T>
constexpr Span<T> MakeSpan(T* ptr, size_t size) noexcept {
return Span<T>(ptr, size);
}
Run Code Online (Sandbox Code Playgroud)
我不明白这个功能.在int&... ExplicitArgumentBarrier做什么?它没有在函数中使用,所以我无法弄清楚它被使用的原因.
举例来说明这是什么'技巧'以及为什么使用它将非常感激.
给出以下代码,我们可以看到,std::string_view当字符串增长超出容量时,它就会失效(这里 SSO 最初生效,然后将内容放入堆上)
#include <iostream>
#include <cassert>
#include <string>
using std::cout;
using std::endl;
int main() {
std::string s = "hi";
std::string_view v = s;
cout << v << endl;
s = "this is a long long long string now";
cout << v << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
hi
#
Run Code Online (Sandbox Code Playgroud)
因此,如果我将 string_view 存储到字符串中,然后更改字符串的内容,我可能会遇到大麻烦。鉴于现有的实现,是否有可能std::string变得更智能string_view?哪个不会面临这样的缺点?我们可以存储一个指向字符串对象本身的指针,然后确定该字符串是否更多地处于 SSO 中并相应地工作。(虽然不确定这如何处理文字字符串,所以也许这就是为什么不这样做的原因? )
我知道这string_view类似于存储返回值,string::c_str()但考虑到我们有这个包装器,std::string我认为很多使用此功能的人不会遇到这个问题。大多数免责声明是为了确保所指向的内容std::string在范围内,但这完全是一个不同的问题。
通过阅读标准,我无法确定以下代码是否违反了 ODR:
// a.h
#ifndef A_HEADER_FILE
#define A_HEADER_FILE
namespace {
int v;
}
inline int get_v() { return v; }
#endif // A_HEADER_FILE
// a.cpp
#include "a.h"
void f() {
int i = get_v();
// ...
}
// b.cpp
#include "a.h"
void g() {
int i = get_v();
// ...
}
Run Code Online (Sandbox Code Playgroud)
(来源:https : //wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file)
据说,get_v()在每个翻译单元中引用了不同的变量,因此它违反了ODR。
这个答案:内联函数和外部链接说内联放松ODR所以我不确定为什么这仍然是一个错误?
如果这是否ODR违规,有人可以将我链接到标准中指定的位置吗?
c++ one-definition-rule inline-functions linkage language-lawyer
c++ ×8
c++11 ×3
c++14 ×3
c++17 ×2
python ×2
python-3.x ×2
templates ×2
apt-get ×1
bash ×1
dockerfile ×1
iterator ×1
linkage ×1
pyodbc ×1
set ×1
sfinae ×1
sqlalchemy ×1
string-view ×1
ubuntu ×1