#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 :: …
在回答了这个问题之后,对于有问题的代码是否是未定义的行为进行了长时间的讨论.这是代码:
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
我是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) 我是Win32编程的新手.我想在两个进程之间创建一个共享内存.我已经使用Memory Mapping功能创建了共享内存.
我的结构看起来像这样:
struct sharedMemory
{
int ans1;
int ans2;
BOOLEAN flag1;
};
Run Code Online (Sandbox Code Playgroud)
现在我能够从不同的进程访问这个共享内存,但是我对如何将锁应用于这个共享内存感到困惑,这样只有一个进程能够访问struct成员变量.
伪代码(这是我的课程):
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>&, …
正常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 的评论:
人们似乎没有抓住重点,所以这里有一个更好的例子来说明为什么这很有用:
Run Code Online (Sandbox Code Playgroud)if constexpr (sizeof...(Ts) > 0 && is_integral_v<first_t<Ts...>>) { /* corresponding logic */ }目前,这需要嵌套的 constexpr ifs
这似乎目前是不可能的,唯一的解决方法是编写嵌套的 ifs。
这一行的含义是什么:
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) 我运行下面的程序,在第一行main之后得到了这个输出:
1
6
1
6
13
任何人都可以解释为什么它进入的建设者Grand和Father两次?
//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) 在研究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 初学者。当我尝试运行以下代码时:
#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++ ×9
c ×1
c++17 ×1
comparison ×1
constexpr ×1
containers ×1
if-statement ×1
list ×1
memory ×1
scanf ×1
stl ×1
templates ×1
vector ×1
visual-c++ ×1