按照芭丝谢芭的要求,并作为一个后续问题"如果一个枚举不适合整体类型会怎样?" :
假设枚举定义如下:
enum foo : unsigned int
{
bar = UINT_MAX,
oops
};
Run Code Online (Sandbox Code Playgroud)
是oops定义的价值还是不是?
MSVS2015编译:
warning C4340: 'oops': value wrapped from positive to negative value
warning C4309: 'initializing': truncation of constant value
warning C4369: 'oops': enumerator value '4294967296' cannot be represented as 'unsigned int', value is '0'
Run Code Online (Sandbox Code Playgroud)
MSVS2015输出:
bar = 4294967295
oops= 0
Run Code Online (Sandbox Code Playgroud)
gcc 4.9.2编译:
9 : note: in expansion of macro 'UINT_MAX'
bar = UINT_MAX,
^
10 : error: enumerator value 4294967296l is …Run Code Online (Sandbox Code Playgroud) 鉴于以下程序:
#include <cstdio>
#include <type_traits>
#include <utility>
struct EmptyClass {};
template <typename T, typename U>
struct Storage {
protected:
union Union {
T t;
U u;
} data;
char flag = false;
};
class AnotherEmptyClass {};
template <typename T, typename U>
class Derived : private Storage<T, U>, public AnotherEmptyClass {};
static_assert(std::is_standard_layout_v<Derived<char, EmptyClass>>);
int main() {
printf("Storage<char, EmptyClass>: %zu\n", sizeof(Storage<char, EmptyClass>));
printf("Derived<char, EmptyClass>: %zu\n", sizeof(Derived<char, EmptyClass>));
printf("Storage<char, char>: %zu\n", sizeof(Storage<char, char>));
printf("Derived<char, char>: %zu\n", sizeof(Derived<char, char>));
}
Run Code Online (Sandbox Code Playgroud)
这在 Linux 上输出 2 …
作为" /sf/ask/2361242901/ " 的后续行动
我问自己,如果促进所有类型(除了一些例外),以较低的排名比int来int进行算术运算可能导致UB在某些情况下.
例如:
unsigned short a = 0xFFFF;
unsigned short b = a*a;
Run Code Online (Sandbox Code Playgroud)
由于unsigned short被提升为int算术运算,这将导致:
unsigned short a = 0xFFFF;
unsigned short b = (int)a*(int)a;
Run Code Online (Sandbox Code Playgroud)
因为(int)0xFFFF*(int)0xFFFF导致溢出,并且有符号类型的溢出是UB:可以将两个无符号短路相乘x,y导致未定义的行为x*y > INT_MAX
更新:
该问题专门针对的int是32位且short为16位的情况.
美好的一天,我正在讨论Bjarne Stroustrup的"C++编程语言",我正面临一段代码,我认为这些代码应该是非法的,但是在文中提出.
我想知道这是否只是一个轻微的疏忽,或者是否有一些我遗漏的东西.
从第3章第63页开始:我们有用户定义的类型Vector,如下所示:
class Vector {
private:
double* elem; // elem points to an array of sz doubles
int sz;
public:
Vector(int s) :elem{new double[s]}, sz{s} // constructor: acquire resources
{
for (int i=0; i!=s; ++i) elem[i]=0; // initialize elements
}
Vector(std::initializer_list<double>)
{
// initialize with a list
}
// ...
void push_back(double)
{
// add element at end increasing the size by one
}
~Vector() {
delete[] elem;
} // destructor: release resources
double& operator[](int i);
int size() …Run Code Online (Sandbox Code Playgroud) int equiv (char, char);
int nmatches(char *str, char comp) {
char c;
int n=0;
while ((c = *str) != 0) {
if (equiv(c,comp) != 0) n++;
str++;
}
return (n);
}
Run Code Online (Sandbox Code Playgroud)
"(c =*str)!= 0"究竟是什么意思?有人可以向我解释或帮我给自己一个正确的条款来搜索解释吗?
我有一个问题,当我尝试访问一个lambda中的主要参数的字符串时,编译器在我尝试使用该字符串调用函数时不会识别它.
这是我的代码:
void removePunctuation(std::vector<std::string> &inTokens,
std::vector<std::string> &outTokens) {
std::for_each(inTokens.begin(), inTokens.end(), [outTokens](std::string s) {
std::string newS = s;
// newS.erase(std::remove_if(newS.begin(), newS.end(), ispunct));
outTokens.push_back(newS);});
}
Run Code Online (Sandbox Code Playgroud)
并产生以下错误:
a2.cpp:114:19: error: no matching member function for call to 'push_back'
outTokens.push_back(newS);});
Run Code Online (Sandbox Code Playgroud)
当我尝试调用一个在调用中使用lambda的字符串参数的函数时,我也在其他函数中遇到这种错误.
任何帮助深表感谢!
我是一名开始学习编码C++的基础学生.我正在为我的大学任务做一个调查程序,在测试之后,我发现子函数的总和值不能正确地与主函数中的值相加.任何一个帮助!
这是代码:
#include <iostream>
using namespace std;
int Q1();
int Q2();
int Q3();
int Q4();
int Q5();
int main()
{
char select;
int E, A, C, N, O;
int extroversion=0, agreeableness=0, conscientiousness=0, neuroticism=0, opennesstoexperience=0;
cout << "This is a Self-Esteem Questionnaire." << endl;
cout << "\nInsert S to start the test (Q to Quit): ";
cin >> select;
select=toupper(select);
if (select=='S')
{
cout << "------------------INSTRUCTIONS-----------------" << endl;
cout << "For each statement 1-50 mark how much you agree" << endl; …Run Code Online (Sandbox Code Playgroud)