小编chr*_*ris的帖子

为什么输出带转换运算符的类不适用于std :: string?

这个工作,打印1:

#include <iostream>

struct Int {
    int i;
    operator int() const noexcept {return i;}
};

int main() {
    Int i;
    i.i = 1;
    std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)

但是,这无法在GCC 4.8.1上编译:

#include <iostream>
#include <string>

struct String {
    std::string s;
    operator std::string() const {return s;}
};

int main() {
    String s;
    s.s = "hi";
    std::cout << s;
}
Run Code Online (Sandbox Code Playgroud)

以下是错误的相关部分:

错误:'operator <<'不匹配(操作数类型是'std :: ostream {aka std :: basic_ostream}'和'String')
std :: cout << s;

SNIP

template std :: basic_ostream <_CharT,_Traits>&std :: …

c++ templates compiler-errors implicit-conversion

5
推荐指数
1
解决办法
7382
查看次数

索引一个新的map元素并且有一些东西读取它分配给它未定义的行为,或者只是未指定?

在回答了这个问题之后,对于有问题的代码是否是未定义的行为进行了长时间的讨论.这是代码:

std::map<string, size_t> word_count;
word_count["a"] = word_count.count("a") == 0 ? 1 : 2;
Run Code Online (Sandbox Code Playgroud)

首先,至少没有具体说明这一点已经确定.结果根据首先评估分配的哪一侧而不同.在我的回答中,我跟踪了四个结果案例中的每一个,其中包括首先评估哪一方的因素以及该元素之前是否存在.

还有一个简短的表格:

(x = 0) = (x == 0) ? 1 : 2; //started as
(x = 0) = (y == "a") ? 1 : 2; //changed to
Run Code Online (Sandbox Code Playgroud)

我声称它更像是这样的:

(x = 0, x) = (x == 0) ? 1 : 2; //comma sequences x, like [] should
Run Code Online (Sandbox Code Playgroud)

最后,我找到了一个似乎对我有用的例子:

i = (++i,i++,i); //well-defined per SO:Undefined Behaviour and Sequence Points
Run Code Online (Sandbox Code Playgroud)

回到原文,我把它分解成相关的函数调用,以便更容易理解:

operator=(word_count.operator[]("a"), word_count.count("a") == 0 …
Run Code Online (Sandbox Code Playgroud)

c++ operator-precedence undefined-behavior sequence-points unspecified-behavior

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

C++ ---错误C2664:'int scanf(const char*,...)':无法将参数1从'int'转换为'const char*'

我是C++的新手,我正在尝试构建这个非常简单的代码,但我不明白为什么会出现这个错误:

Error   1   error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'
Run Code Online (Sandbox Code Playgroud)

这是代码:

// lab.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h> 

int main(int argc, char* argv[])
{
    int row = 0;
    printf("Please enter the number of rows: ");
    scanf('%d', &row);
    printf("here is why you have entered %d", row);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ scanf

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

如何确保一次只有一个进程访问共享内存

我是Win32编程的新手.我想在两个进程之间创建一个共享内存.我已经使用Memory Mapping功能创建了共享内存.

我的结构看起来像这样:

struct sharedMemory
{
  int ans1;
  int ans2;
  BOOLEAN flag1;
};
Run Code Online (Sandbox Code Playgroud)

现在我能够从不同的进程访问这个共享内存,但是我对如何将锁应用于这个共享内存感到困惑,这样只有一个进程能够访问struct成员变量.

c++ win32-process visual-c++

3
推荐指数
1
解决办法
1640
查看次数

使用 == 运算符的自定义类比较向量

伪代码(这是我的课程):

struct cTileState   {
        cTileState(unsigned int tileX, unsigned int tileY, unsigned int texNr) : tileX(tileX), tileY(tileY), texNr(texNr) {}
        unsigned int tileX;
        unsigned int tileY;
        unsigned int texNr;

        bool operator==(const cTileState & r)
        {
            if (tileX == r.tileX && tileY == r.tileY && texNr == r.texNr) return true;
            else return false;
        }
    };
Run Code Online (Sandbox Code Playgroud)

然后我有两个容器:

       std::list < std::vector <cTileState> > changesList;  //stores states in specific order
       std::vector <cTileState> nextState;
Run Code Online (Sandbox Code Playgroud)

在程序的某个地方,我想在我的状态交换函数中做到这一点:

      if (nextState == changesList.back()) return;
Run Code Online (Sandbox Code Playgroud)

但是,当我想编译它时,我有一些对我来说毫无意义的错误,例如:

/usr/include/c++/4.7/bits/stl_vector.h:1372:58: 来自 'bool std::operator==(const std::vector<_Tp, _Alloc>&, …

c++ comparison containers list vector

3
推荐指数
1
解决办法
3055
查看次数

逻辑短路是否不适用于 if-constexpr?

正常if情况下,短路工作。

但是,如果尝试短路 if-constexpr 不起作用:

#include <iostream>
template <typename ... Args>
void foo(Args... args) {
    std::string a; 
    // for the call of foo, sizeof...(args) = 0, so a > 2 shouldn't be evaluated.
    if constexpr (sizeof...(args) == 0 || a > 2) {
        std::cout << "ASD" << '\n';
    }
}

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

演示

编辑:似乎很多评论都与我的尝试有所不同。我将引用@chris 的评论:

人们似乎没有抓住重点,所以这里有一个更好的例子来说明为什么这很有用:

if constexpr (sizeof...(Ts) > 0 && is_integral_v<first_t<Ts...>>) { 
    /* corresponding logic */ 
}
Run Code Online (Sandbox Code Playgroud)

目前,这需要嵌套的 constexpr ifs

这似乎目前是不可能的,唯一的解决方法是编写嵌套的 ifs。

c++ if-statement constexpr c++17

3
推荐指数
2
解决办法
462
查看次数

C++中的new运算符和数组

这一行的含义是什么:

int* p = new int[2,2];
Run Code Online (Sandbox Code Playgroud)

在以下c ++代码中?

#include <iostream>
using namespace std;

int main()
{
    int* p = new int[2,2];
}
Run Code Online (Sandbox Code Playgroud)

c++ memory

2
推荐指数
1
解决办法
113
查看次数

构造函数的顺序 - 链继承

我运行下面的程序,在第一行main之后得到了这个输出:

1
6
1
6
13

任何人都可以解释为什么它进入的建设者GrandFather两次?

//base class 
class Grand{
public:
    Grand(){cout<<"1"<<endl;}
    Grand(const Grand & g){cout<<"2"<<endl;}
    virtual ~Grand  (){cout<<"3"<<endl;}
    virtual void fun1(Grand g){cout<<"4"<<endl;}
    virtual void fun3(){cout<<"5"<<endl;}
private:
    int m_x;
    };



//derived class 
    class Father: public Grand{
    public:
        Father(){cout<<"6"<<endl;}
        Father(const Father & f){cout<<"7"<<endl;}
        ~Father(){cout<<"8"<<endl;}
        void fun1(){cout<<"9"<<endl;}
        void fun2(Father & f){
            cout<<"10";
            f.fun3();
        }
        void fun3(){
            cout<<"11"<<endl;
        }
        virtual void fun4(){
            cout<<"12"<<endl;
        }
    };


sing namespace std;

//derived class 
        class Son: public Father{
        public:
            Son(){cout<<"13"<<endl;}
            Son(const Son& …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
675
查看次数

为什么parens会阻止宏观替代?

在研究windows min/ maxmacro问题的解决方案时,我找到了一个我非常喜欢的答案,但我不明白它为什么会起作用.C++规范中是否有一些内容表明在parens中不会发生宏替换?如果是这样的话呢?这只是其他东西的副作用,还是那种以这种方式工作的语言?如果我使用额外的parens max宏不会导致问题:

(std::numeric_limits<int>::max)()
Run Code Online (Sandbox Code Playgroud)

我正在大规模的MFC项目中工作,有一些Windows库使用这些宏,所以我不想使用这个#undef技巧.

我的另一个问题是这个.#undef max.cpp文件中是否只影响它在其中使用的文件,或者它是否会max为其他编译单元取消定义?

c++ stl

2
推荐指数
1
解决办法
178
查看次数

序列点警告说明

我是 C 初学者。当我尝试运行以下代码时:

#include <stdio.h>

    int main(void) {

    int a = 3, b;

    b = printf("%d %d", a, a++);
    a = printf(" %d", b);
    printf(" %d", a);

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

它向我展示:

error: operation on 'a' may be undefined [-Werror=sequence-point]
b = printf("%d %d", a, a++);
                        ^
Run Code Online (Sandbox Code Playgroud)

但在这里我只改变了一次的值。那为什么会出现序列点错误呢?

我正在使用 -Wall -Werror 标志。

c sequence-points

2
推荐指数
1
解决办法
4191
查看次数