似乎sqlite不允许我在只读数据库中创建临时视图。我想念什么吗?如果是TEMPORARY,我认为数据库连接模式无关紧要。
我什至指定“ PRAGMA temp_store = MEMORY”-它没有帮助。
除了使用视图,还有其他合理的选择吗?
在提出这个问题的时候,我学会了对一个临时对象的const引用在C++中是有效的:
int main ()
{
int a = 21;
int b = 21;
//error: invalid initialization of non-const reference
//int & sum = a + b;e [...]
//OK
int const & sum = a + b;
return sum;
}
Run Code Online (Sandbox Code Playgroud)
但在下面的示例中,const引用refnop引用了一个被销毁的临时对象.我想知道为什么?
#include <string>
#include <map>
struct A
{
// data
std::map <std::string, std::string> m;
// functions
const A& nothing() const { return *this; }
void init() { m["aa"] = "bb"; }
bool operator!= (A const& a) const …Run Code Online (Sandbox Code Playgroud) 哪种编码风格更适合编译器优化?特别是,我感兴趣1)最小化立即扔掉的临时值的数量和2)自动矢量化,即生成用于算术的SIMD指令.
假设我有这个结构:
#define FOR_EACH for (int i = 0; i < N; ++i)
template<typename T, unsigned N>
struct Vector {
void scale(T scalar) {
FOR_EACH v[i] *= scalar;
}
void add(const Vector<T, N>& other) {
FOR_EACH v[i] += other.v[i];
}
void mul(const Vector<T, N>& other) {
FOR_EACH v[i] *= other.v[i];
}
T v[N];
};
Run Code Online (Sandbox Code Playgroud)
此结构的示例用法:
Vector<int, 3> v1 = ...;
Vector<int, 3> v2 = ...;
v1.scale(10);
v1.add(v2);
v1.mul(v2);
Run Code Online (Sandbox Code Playgroud)
这是一种可变的方法.
另一种不可变的方法可能如下所示:
template<typename T, unsigned N>
struct Vector {
Vector(const Vector<T, N>& …Run Code Online (Sandbox Code Playgroud) 我有一个类型的对象,例如,std::vector<int> v;
现在,比方说,我想验证v释放所有内部内存.
在C++ 11 shrink_to_fit()方法之前,推荐/保证的方法是swap()使用std::vector<>相同类型的空.
但是,我不想指定对象的类型.我可以decltype用来指定类型,所以我想写这样的东西:
std::vector<int> v;
// use v....
v.swap(decltype(v)()); // Create a temporary of same type as v and swap with it.
^^
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码不起作用.我似乎无法创建一个decltype(v)带有空ctor 的临时类型(在本例中).
是否有其他语法来创建这样的临时?
给定以下结构:
struct ABC
{
ABC(){cout << "ABC" << endl;}
~ABC() noexcept {cout << "~ABC" << endl;}
ABC(ABC const&) {cout << "copy" << endl;}
ABC(ABC&&) noexcept {cout << "move" << endl;}
ABC& operator=(ABC const&){cout << "copy=" << endl;}
ABC& operator=(ABC&&) noexcept {cout << "move=" << endl;}
};
Run Code Online (Sandbox Code Playgroud)
输出:
std::pair<std::string, ABC> myPair{{}, {}};
Run Code Online (Sandbox Code Playgroud)
是:
ABC
copy
~ABC
~ABC
Run Code Online (Sandbox Code Playgroud)
而输出:
std::pair<std::string, ABC> myPair{{}, ABC{}};
Run Code Online (Sandbox Code Playgroud)
是:
ABC
move
~ABC
~ABC
Run Code Online (Sandbox Code Playgroud)
在试图理解两者之间的差异时,我认为我已经确定第一种情况是使用复制列表初始化,而第二种情况使用未命名临时的直接列表初始化(数字7和2分别在这里) :http://en.cppreference.com/w/cpp/language/list_initialization).
搜索类似的问题我发现:为什么标准区分直接列表初始化和复制列表初始化?并且:复制列表初始化是否从概念上调用了复制ctor?.
这些问题的答案讨论了这样一个事实:对于复制列表初始化,使用显式构造函数会使代码格式错误.事实上,如果我将ABC的默认构造函数显式化,我的第一个例子将不会编译,但这可能是(可能)另一个问题.
所以,问题是:为什么临时复制在第一种情况下,但在第二种情况下移动?什么阻止它在复制列表初始化的情况下被移动?
作为注释,以下代码: …
当您尝试分配给临时对象时,是否有任何编译器选项允许您收到警告?
例子:
struct S {
S op() { return S(); }
};
int main() {
S s;
s.op() = s; // assign to temporary. Wants to warn here.
}
Run Code Online (Sandbox Code Playgroud)
我知道你可以声明opas的返回类型const来防止这种情况,但现在我感兴趣的只是编译器选项。
您可以使用任何流行的现代编译器。
我想将一个临时对象(例如std :: string)传递给我的对象的构造函数:
class MyClass{
public:
MyClass(string a):
a(a)
{
}
string a;
};
int main(int argc, char *argv[]){
MyClass a(string());
cout<<a.a<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:28:11: error: request for member ‘a’ in ‘a’, which is of non-class type ‘MyClass(std::string (*)()) {aka MyClass(std::basic_string<char> (*)())}’
Run Code Online (Sandbox Code Playgroud)
如果我将任何东西传递给临时对象的构造函数(例如string("")),一切正常.为什么?
在看到本地引用const可能会延长临时的生命之后,我遇到了有条件地将本地reference-to-const绑定到函数参数或函数调用的临时结果的需要,即:
class Gizmo
{
// Rule of Five members implemented
};
Gizmo Frobnicate(const Gizmo& arg);
void ProcessGizmo(const Gizmo& arg, bool frobnicate)
{
const Foo& local = frobnicate ? Frobnicate(arg) : arg;
// Perform some work on local
}
Run Code Online (Sandbox Code Playgroud)
一个实际的例子:boolean指定是否压缩缓冲区,并且您希望编写以local任一方式操作的统一代码.
上面的例子,但是,在调用小玩意儿的拷贝构造函数arg时frobnicate是false.我设法通过更改Frobnicate(arg)为避免复制构造函数的调用static_cast<const Gizmo&>(Frobnicate(arg)).
我的问题变成:三元运算符如何与关于将本地引用到const绑定到临时的规则进行交互?我的解决方案是否合法且表现良好?
请考虑以下代码段:
struct foo { };
template <typename F>
struct impl : F
{
impl(F&& f) : F{std::move(f)} { }
auto get() { return (*this)(); }
};
template <typename X>
auto returner(X&& x)
{
return impl{[&x]{ return x; }};
// ^~
}
int main()
{
auto x = returner(foo{}).get();
}
Run Code Online (Sandbox Code Playgroud)
是否保证foo{}在returner(foo{}).get()表达的整个过程中都会存活?
或者只是foo{}为了活着returner(foo{}),因此在调用时会导致未定义的行为impl::get()?
标准在[class.temporary]中说:
临时对象被破坏,作为评估(词法上)包含创建它们的点的完整表达式的最后一步.
一个完整的表达是
一个未评估的操作数,
一个常数表达式,
init-declarator或mem-initializer,包括初始化程序的组成表达式,
调用在临时对象([class.temporary])以外的对象的生命周期结束时生成的析构函数,或者
一个表达式,它不是另一个表达式的子表达式,也不是完整表达式的一部分.
我不知道是否完全表达有关 …
考虑以下代码 -
#include <iostream>
#include <stdio.h>
const int & retRef() {
return 6;
}
int main()
{
const int& k = retRef();
printf("Value: %d\n", k);
printf("Address: %p\n", &k);
printf("Value: %d\n", k);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是 -
Value: 6
Address: 0x7ffd45bf544c
Value: 32692
Run Code Online (Sandbox Code Playgroud)
为什么在打印变量的地址后值发生了变化k?如果我更换线const int& k = retRef()用const int& k = 6;的输出为预期.
为什么这种行为不同?提前致谢