我有一些复杂的模板代码,其中复制构造函数OPC
被调用,即使我只是创建一个引用OPC
(实际的实例是OP_S
,作为子类OPC
,不应该导致复制构造调用).
我正在使用gcc 4.6.1
代码如下.
#include <stdio.h>
class OPC
{
public:
OPC() { }
OPC( const OPC& f ) {
fprintf( stderr, "CC called!!!\n" );
}
};
template<class T>
class SL : public T
{ };
template<class T>
class S : public SL<T>
{ };
class OP_S : public S<OPC>
{ };
class TaskFoo
{
public:
TaskFoo( OPC& tf ) :
m_opc( tf ),
m_copc( tf )
{ }
OPC& getOPC() { return …
Run Code Online (Sandbox Code Playgroud) 我可以使用我的g ++ 4.4指定-std = c ++ 0x进行编译,初始化程序列表是正确的,我可以使用它们(在c ++ 98中我不能)但在尝试使用auto关键字时仍然会出错:
std::list< std::vector<int> > li2;
li2.push_back({1, 2, 3}); //push_back vector
li2.push_back({4, 2, 6}); //again, vector implicitly
for (auto& vv : li2) {
for (auto &i : v)
printf("element: %d\n", 8);
}
Run Code Online (Sandbox Code Playgroud)
所以我假设我不能在g ++ 4.4中使用C++ 11函数.由于与CUDA的兼容性,我有4.4.
我刚刚测试了这段代码.
vector<bool> v(5, true);
if(v.back())cout<<"====="<<endl;
auto b1 = v.back();
b1 = false;
cout<<&b1<<endl;
if(v.back())cout<<"*********"<<endl;
Run Code Online (Sandbox Code Playgroud)
我的问题如下:
auto
正在改变bool向量v
?vector<bool>
是没有一个标准的STL容器,并通过解决它的元素&v[4]
将无法正常工作(因为你不能满足位的地址),如果b1
通过参考声明v.back()
,为什么我可以解决b1
的&b1
?auto
有这种行为?是否auto c1 = v.begin()
以后做c1 = (++v.begin())
会改变v.begin()
?// (1)
template<typename T>
T add1(T a, T b)
{
return a + b;
}
// (2)
auto add2 = [](auto a, auto b)
{
return a + b;
};
Run Code Online (Sandbox Code Playgroud)
在这个简单的例子中,我想知道哪个实现更好:
auto [x, y] = div_t{ 1, 0 };
Run Code Online (Sandbox Code Playgroud)
从答案的代码看起来这像是tie
对div_t
结构.我希望有人能解释这里发生的事情.完整的功能代码如下:
constexpr bool first_quot() {
auto [x, y] = std::div_t{1, 0};
(void)y;
return x;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码行,效果很好:
const auto& dict = m_DictionaryAbbreviationsAndEnglish.Content;
Run Code Online (Sandbox Code Playgroud)
现在我想引入一个"if-then"子句,但编译器告诉我"无法推断'auto'类型(需要使用initalizer):
const auto& dict;
if (uSkipAbbreviationsAndEnglish)
{
dict = m_DictionaryNoAbbreviationsNoEnglish.Content();
}
else
{
dict = m_DictionaryAbbreviationsAndEnglish.Content();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我像这样初始化它时......
const auto& dict=NULL;
Run Code Online (Sandbox Code Playgroud)
...,我无法使用此类代码分配"dict":
dict = m_DictionaryNoAbbreviationsNoEnglish.Content();
Run Code Online (Sandbox Code Playgroud)
错误是"表达式必须是可修改的l值".
任何人都可以告诉我如何正确地做到这一点?
谢谢.
ps:内容是这样的:
map<wstring,wstring> &clsTranslations::Content()
{
return m_content;
}
Run Code Online (Sandbox Code Playgroud) 所以,如果我有以下向量,其中包含几个指向int的指针:
std::vector<MyClass*> list;
Run Code Online (Sandbox Code Playgroud)
我可以稍后使用以下内容迭代它:
for (auto & (*item) : list)
{
item.member = something; //Goal is to require no dereferencing here
}
Run Code Online (Sandbox Code Playgroud)
使用对列表内容的值的引用比使用它们的指针稍微方便一点.
我有这个代码:
#include <iostream>
#include <vector>
template<typename T>
void print_size(std::vector<T> a)
{
std::cout << a.size() << '\n';
}
int main()
{
std::vector<int> v {1, 2, 3};
print_size(v);
auto w = {1, 2, 3};
// print_size(w); // error: no matching function for call to 'print_size'
// candidate template ignored: could not match 'vector' against 'initializer_list'
}
Run Code Online (Sandbox Code Playgroud)
...编译和运行没有任何问题.但是,如果我启用注释掉的行,则会产生错误no matching function for call to 'print_size'
.
我想知道在C++ 11及更高版本中编写此代码的正确方法是什么.
如果我声明:
string s = "ARZ";
Run Code Online (Sandbox Code Playgroud)
然后运行以下代码:
for (auto& c : s) {
cout << (void*)&c << endl;
}
Run Code Online (Sandbox Code Playgroud)
结果将对应于的地址s[0]
,s[1]
和s[2]
分别。
如果我删除&
并运行:
for (auto c : s) {
cout << (void*)&c << endl;
}
Run Code Online (Sandbox Code Playgroud)
的地址c
始终相同。
大概c
只是指向向量的指针,它的值sizeof(char)
随每个循环而增加,但是我发现很难理解为什么我不需要写*c
访问字符串char值的原因。
最后,如果我运行:
for (auto c: s) {
c='?';
cout << c << endl;
}
Run Code Online (Sandbox Code Playgroud)
它打印出3个问号。
我发现很难理解c
实际上是什么?
我正在尝试使用auto作为返回的lambda函数的返回类型。这是一个最小的示例,它演示了我遇到的一个问题:
#include <iostream>
#include <memory>
auto
get_func()
{
auto i = std::make_unique<int>(2);
if (*i == 1) {
return [i=std::move(i)]() {
return *i;
};
}
return [](){ return 2; };
}
int
main(int argc, char* argv[])
{
auto func = get_func();
std::cout << "val: " << func() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的Mac上,出现以下编译错误:
$ g++ -g -Wall -Werror -std=c++17 test.cc -o test
test.cc:13:5: error: 'auto' in return type deduced as '(lambda at test.cc:13:12)' here but deduced as '(lambda at test.cc:9:16)' in …
Run Code Online (Sandbox Code Playgroud)