我只是学习C++而我正在使用Accelerated C++.
在向量示例中,编写器使用以下代码;
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size;
Run Code Online (Sandbox Code Playgroud)
我知道typedef vector<double>::size_type vec_sz;是这样他不必写下一个命令vector<double>::size_type size = homework.size;,但我的问题是为什么他不是只是声明size为整数?
int size = homework.size;
Run Code Online (Sandbox Code Playgroud)
这是因为我们正在使用向量吗?
如果是这样,这是否意味着向量迭代器返回的值不能存储在常规变量中?
class B {
public:
virtual void f(){
printf("B\n");
}
};
class D : public B {
public:
void f() {
printf("D\n");
}
};
int main(void)
{
B* d = new D();
d->f();
auto b = *d;
b.f();
}
Run Code Online (Sandbox Code Playgroud)
因为d->f();,输出是D.这是正确的.但是b.f();,输出是B.这是正确的吗?
关于从函数返回并绑定到rvalue/const左值引用的对象的生命周期的延长,我有一些不清楚的信息.来自这里的信息.
不会扩展对return语句中函数的返回值的临时绑定:它会在返回表达式的末尾立即销毁.这样的函数总是返回一个悬空引用.
如果我理解正确,引用声称返回语句返回的对象的生命周期是不可扩展的.但最后一句话表明,它只适用于返回引用的函数.
在海湾合作委员会,从下面的代码我得到以下输出:
struct Test
{
Test() { std::cout << "creation\n"; }
~Test() { std::cout << "destruction\n"; }
};
Test f()
{
return Test{};
}
int main()
{
std::cout << "before f call\n";
Test && t = f();
std::cout << "after f call\n";
}
Run Code Online (Sandbox Code Playgroud)
before f call
creation
after f call
destruction
Run Code Online (Sandbox Code Playgroud)
所以看起来生命延长了.
是否应延长与此类参考相关的临时对象的生命周期?您还可以提供更明确的信息来源吗?
template<class Key1, class Key2, class Type> class DualMultimapCache
{
public:
std::list<std::reference_wrapper<Type>> get(Key1 const & key);
std::list<std::reference_wrapper<Type>> get(Key2 const & key);
template<class ...Args> Type & put(Key1 const & key, Args const & ...args);
template<class ...Args> Type & put(Key2 const & key, Args const & ...args);
};
Run Code Online (Sandbox Code Playgroud)
在这里,我有一个类的公共接口.基础数据结构无关紧要.一切都将只是正常工作时Key1,并Key2有不同的类型.如果它们最终成为相同的类型,则过载可能是不可能的.我这是对的吗?
如果我是,有没有办法分离过载,同时保持签名尽可能干净?
编辑:这是一个更深入的样本
template<class Key1, class Key2, class Type> class DualMultimapCache
{
public:
std::list<std::reference_wrapper<Type>> get(Key1 const & key);
std::list<std::reference_wrapper<Type>> get(Key2 const & key);
template<class ...Args> Type & put(Key1 const & key, …Run Code Online (Sandbox Code Playgroud) c++ templates overloading overload-resolution class-template
在试图解决一个问题,我开始思考这个-给予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) 最近有一个恭维问题,由此片段说明:
struct Base
{
};
template<typename T>
struct A : Base
{
A(){}
A(Base&&) {}
};
A<int> foo()
{
A<double> v;
return v;
}
int main()
{
auto d = foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Gcc说没关系,但是clang不同意并说"候选构造函数不可行:第一个参数A(Base &&){}"没有已知的从'A'到'Base &&'的转换,请亲自看看:https://godbolt.org/Z/Y7mwnU
任何一种读者是否能够帮助支持任何一种观点?
我正在尝试使用可选参数创建模板化函数,但我无法理解编译失败的原因.这是我的测试(人为)代码:
#include <iostream>
#include <vector>
template <class UnaryPredicate>
int GetCountIf(std::vector<int> v, UnaryPredicate pred = [](auto) { return true; }) {
int count=0;
for (auto i: v) {
if (pred(i)) {
count++;
}
}
return count;
}
int main() {
auto v = std::vector<int>{0, 1, 2, 3, 4, 5};
std::cout << "NumOddElements=" << GetCountIf(v, [](auto val) { return (val % 2 == 1); }) << '\n';
// std::cout << "NumElements=" << GetCountIf(v) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
只有在我GetCountIf()使用两个参数调用时,代码才会编译.如果我尝试仅传递1个参数,编译将失败并显示以下错误:
main.cpp:18:34:错误:没有匹配函数来调用'GetCountIf'std
:: cout …
它是错误还是标准允许?
#include <iostream>
#include <map>
#include <unordered_map>
int main() {
std::unordered_map<int,int> mm {{44,44}, {33.3, 54}, {222.2,222.2}};
for(auto& [f,s] :mm) {
std::cout<<f<<" - "<<s<<std::endl;
}
std::map<int,int> m {{44,44}, {33.3, 54}, {222.2,222.2}};
for(auto& [f,s] :m) {
std::cout<<f<<" - "<<s<<std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在wandbox.org 上使用 clang10 和 gcc10对其进行测试。std::set和没有这样的问题std::unordered_set。
void Foo1(string_view view) {
...
}
string str = "one two three";
Foo1("one two three"); // Implicitly convert char* to string_view
Foo1(str);
Run Code Online (Sandbox Code Playgroud)
我想知道哪个构造函数将 char* 隐式转换为 string_view ,哪个构造函数将字符串隐式转换为 string_view ?
我知道构造函数 (4) 将 const char* 转换为 string_view 但我传递的是 char*。
当使用不同的右值引用类型调用重载函数时,我在代码中遇到了意外行为。下面的代码片段演示了这个问题:
#include <iostream>
void foo(int&& x)
{
std::cout << "int Rvalue: " << x << std::endl;
}
void foo(float&& x)
{
std::cout << "float Rvalue: " << x << std::endl;
}
int main()
{
int int_x = 1;
float float_x = 5.14f;
foo(int_x);
foo(float_x);
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)float Rvalue: 1 int Rvalue: 5
我没有得到预期的输出(应该是“int Rvalue:1”和“float Rvalue:5”),而是观察到相反的顺序。我发现这种行为令人困惑,并且希望了解为什么会发生这种情况。
我确保理解右值引用的概念以及基于它们的重载如何工作。我也尝试过搜索类似的问题,但没有找到任何直接解决此特定情况的问题。
任何指导或解释将不胜感激。预先感谢您的帮助!