小编son*_*yao的帖子

当读取错误的类型时,cin会覆盖我的初始化值?

所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:

#include "..\std_lib_facilities.h"

int main()
{
    cout << "Please enter your first name and age\n";
    string first_name = "???"; // string variable
                               // ("???” means “don’t know the name”)
    int age = -1;              // integer variable (1 means “don’t know the age”)
    cin >> first_name >> age;  // read a string followed by an integer
    cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)

当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?

编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …

c++ cin language-lawyer c++11

4
推荐指数
1
解决办法
269
查看次数

为什么我们不能将"="用于shared_ptr或unique_ptr?

我知道语法得到编译错误.

std::shared_ptr<int> p = new int(5);

31 41 E:\temprory (delete it if u like)\1208.cpp [Error] conversion from 'int*' to non-scalar type 'std::shared_ptr<int>' requested
Run Code Online (Sandbox Code Playgroud)

但没关系

std::shared_ptr<int> p(new int(5));
Run Code Online (Sandbox Code Playgroud)

同样的unique_ptr;

但我不知道为什么禁止它.

c++ initialization shared-ptr unique-ptr c++11

4
推荐指数
2
解决办法
579
查看次数

如何使用布尔模板参数启用成员函数?

我希望一个类具有 , 的两种不同实现push,并根据布尔模板参数进行选择。我尝试使用本答案中描述的 SFINAE 原则,如下所示:

template<class T, bool foo=true>
class Bar {
  template <>
  typename std::enable_if<foo>::type
  push(const T& value) { /* one implementation */}

  template <>
  typename std::enable_if<!foo>::type
  push(const T& value) { /* another implementation */ } 
}
Run Code Online (Sandbox Code Playgroud)

但是,我push在 gcc 下收到“无法专门化类范围内的函数”的错误,我不明白为什么。尽管我的代码与链接答案中的代码不完全一样,但它似乎非常相似,我无法发现关键区别。

我还尝试使用与此答案中建议的语法类似的语法,但它也不起作用(错误是“不能重新声明类成员”):

  template <bool enable=foo>
  typename std::enable_if<enable>::type
  push(const T& value) { /* one implementation */}

  template <bool enable=!foo>
  typename std::enable_if<enable>::type
  push(const T& value) { /* another implementation */ } 
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c++ templates sfinae

4
推荐指数
1
解决办法
1324
查看次数

如何在c ++中解释"const int*const&variable"

当一个将两个变量别名为

int a;
const int &b = a;
Run Code Online (Sandbox Code Playgroud)

这两个变量实际上是相同的,因此应用于变量的任何更改a也会应用于变量b.但是,当使用指针完成相同的技巧时,它似乎无法以相同的方式工作,如以下程序所示:

#include <iostream>
int main(void) {

    int *a = (int*) 0x1;
    const int *const &b = a;// Now b should be an alias to a.
    a = (int*) 0x2;// This should change b to 0x2.
    std::cout << b << "\n";// Outputs 0x1 instead of the expected value of 0x2.

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在变量a似乎不是变量的别名b,但为什么呢?

c++ pointers const reference

4
推荐指数
2
解决办法
503
查看次数

重载operator = break std :: sort

可能是一个骗局,但我找不到它.

在敲击键盘两天之后,我发现重载equals运算符(operator=)显然会中断std::sort.也许我的输operator=错不正确?这是我的MCVE:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>

struct Person
{
  std::string name;
  uint32_t age;

  bool operator< (const Person& p)
  {
    return this->age < p.age;
  }

  Person operator= (const Person& p)
  {
    Person newP;
    newP.name = p.name;
    newP.age = p.age;

    return newP;
  }

  static bool SortPeople(const Person& p1, const Person& p2)
  {
    return p1.age < p2.age;
  }
};

void PrintPeople(const std::vector<Person>& people)
{
  std::cout << "============ people begin" …
Run Code Online (Sandbox Code Playgroud)

c++ assignment-operator c++11

4
推荐指数
1
解决办法
185
查看次数

将std :: unique_ptr重置为指向数组的指针有什么问题?

我看到一些代码片段如下:

std::unique_ptr<uint8_t> mCache;
mCache.reset(new uint8_t[size]);
Run Code Online (Sandbox Code Playgroud)

有人告诉我这个代码有一些问题.谁能给我一些细节?

c++ smart-pointers unique-ptr c++11

4
推荐指数
1
解决办法
448
查看次数

为什么 std::distance() for std:list&lt;int&gt;::iterator 当 last 在 first 之前不返回负数?

std::distance在 上给我一个圆形距离std::list,而不是相对距离。为什么?

 #include <list>                                                                 
 #include <iostream>                                                             
 #include <iterator>                                                             

 using namespace std;                                                            

 int main(){                                                                     
     list<int> derp = {1,2,3,4};                                                 
     auto begin = derp.begin();                                                  
     auto end = derp.end();                                                      
     end--;                                                                      
     cout << distance(end, begin) << endl;                                       
     cout << distance(begin, end) << endl;                                       
}             
Run Code Online (Sandbox Code Playgroud)

当我运行它时,会发生以下输出:

2
3
Run Code Online (Sandbox Code Playgroud)

我期望以下内容:

-3
3
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c++ iterator stl stdlist undefined-behavior

4
推荐指数
1
解决办法
401
查看次数

std::basic_string 作为函数模板的参数不能从 const char* 推导出来

为什么std::basic_string作为函数模板的参数 put 会推演失败const char*,而直接构造却可以推演成功?

#include <string>
#include <iostream>
template<class Char/*, class Traits, class Allocator*/>   
                      //^doesn't matter whether the second and third template parameter is specified
void printString(std::basic_string<Char/*, Traits, Allocator*/> s)
{
    std::cout << s;
}
int main()
{
    printString("hello");    //nope
    std::basic_string s{ "hello" };//works
}
Run Code Online (Sandbox Code Playgroud)

我在这里找到了相关帖子,但答案没有解释背后的原因

c++ templates implicit-conversion template-argument-deduction

4
推荐指数
1
解决办法
408
查看次数

转发引用不是被推导为 r 值引用吗?

我有一个关于转发引用的具体问题。(我认为)我理解 r 值引用和std::move,但我无法理解转发引用:

#include <iostream>
#include <utility>

template <typename T> class TD; // from "Effective Modern C++"

void consume(const int &i) { std::cout << "lvalue\n"; }
void consume(int &&i)      { std::cout << "rvalue\n"; }

template <typename T>
void foo(T&& x) {
//    TD<decltype(x)> xType; - prints int&&
    consume(x);
}

int main() {
    foo(1 + 2);
}
Run Code Online (Sandbox Code Playgroud)

Tint,没关系。如果x是 type int&&,为什么它打印“lvalue”而我们需要std::forward?我的意思是,从哪里转换int&&const int&这里?

c++ perfect-forwarding c++11 forwarding-reference

4
推荐指数
1
解决办法
115
查看次数

取消引用函数指针的值是什么意思

#include <iostream>

void PrintTheValue(int(*func)(int a));

int main(int argc, char **argv) {
    PrintTheValue([](int a) {return a; });
    
    return 0;
}

void PrintTheValue(int(*func)(int a)) {
    std::cout << *func << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

在我理解 的概念中func,它将是一个指向按值传递的 int 的指针。但在这种情况下,我传递了一个似乎没有在任何地方被调用的 lambda。(所以根本没有任何价值?)

当我运行它时,它不会破坏程序,而是打印00EE6F80.

这个地址是什么意思?我不知道如何解释它。

c++ function-pointers

4
推荐指数
1
解决办法
72
查看次数