standards-compliance

What is going on with 'gets(stdin)' on the site coderbyte?

天涯浪子 提交于 2019-12-27 17:07:41
问题 Coderbyte is an online coding challenge site (I found it just 2 minutes ago). The first C++ challenge you are greeted with has a C++ skeleton you need to modify: #include <iostream> #include <string> using namespace std; int FirstFactorial(int num) { // Code goes here return num; } int main() { // Keep this function call here cout << FirstFactorial(gets(stdin)); return 0; } If you are little familiar with C++ the first thing * that pops in your eyes is: int FirstFactorial(int num); cout <<

MERGE to target columns using source rows?

白昼怎懂夜的黑 提交于 2019-12-25 17:44:43
问题 I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID INTEGER NOT NULL, body_part_name VARCHAR(5) NOT NULL CHECK (body_part_name IN ('hair', 'eye', 'teeth')), color VARCHAR(20) NOT NULL, UNIQUE (color, body_part_name, person_ID) ); INSERT INTO SourceBodyPartColors (person_ID, body_part_name, color) VALUES (1, 'eye', 'blue'), (1, 'hair', 'blond'), (1, 'teeth', 'white'), (2, 'hair', 'white'), (2, 'teeth', 'yellow'), (3, 'hair', 'red'); Sadly, the

Verifying code against template patterns using reflection

感情迁移 提交于 2019-12-25 03:41:34
问题 I am working on a large project where a base class has thousands of classes derived from it (multiple developers are working on them). Each class is expected to override a set of methods. I first generated these thousands of class files with a code template that conforms to an acceptable pattern. I am now writing unit tests to ensure that developers have not deviated from this pattern. Here is a sample generated class: // Base class. public abstract partial class BaseClass { protected

Template access of symbol in unnamed namespace

谁都会走 提交于 2019-12-24 08:25:53
问题 We are upgrading our XL C/C++ compiler from V8.0 to V10.1 and found some code that is now giving us an error, even though it compiled under V8.0. Here's a minimal example: test.h: #include <iostream> #include <string> template <class T> void f() { std::cout << TEST << std::endl; } test.cpp: #include <string> #include "test.h" namespace { std::string TEST = "test"; } int main() { f<int>(); return 0; } Under V10.1, we get the following error: "test.h", line 7.16: 1540-0274 (S) The name lookup

Multiple user-defined conversions on initialization

我是研究僧i 提交于 2019-12-23 12:25:58
问题 I am aware of the fact that C++ allows only a single user-defined implicit conversion when converting between types. However, I recently came across a situation where it seems like double user-defined implicit conversions are allowed on initialization. Consider the following classes: //fractions class Rational { public: int num, den; // default constructor, etc. Rational(int n) : num(n), den(1) {} // NOT explicit // arithmetic and compound assignment defined between two Rational's. }; /

How important is standards-compliance?

拜拜、爱过 提交于 2019-12-23 07:48:42
问题 For a language like C++ the existence of a standard is a must. And good compilers try their best (well, most of the good compilers, at least) to comply. Many compilers have language extensions, some of which are allowed by the standard, some of which are not. Of the latter kind 2 examples: gcc's typeof microsoft's compilers allow a pure virtual function declaration to have both a pure-specifier(=0) and a definition (which is prohibited by the standard - let's not discuss why, that's another

Are tokens after #endif legal?

て烟熏妆下的殇ゞ 提交于 2019-12-23 07:46:10
问题 I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not: #ifndef FOO_H_ #define FOO_H_ // note, FOO_H_ is not a comment: #endif FOO_H_ I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strange because apparently I've not done the comment method for a while. Is this bad practice that I should go back through all of my headers and fix (it's a cross

Are tokens after #endif legal?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 07:44:04
问题 I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not: #ifndef FOO_H_ #define FOO_H_ // note, FOO_H_ is not a comment: #endif FOO_H_ I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strange because apparently I've not done the comment method for a while. Is this bad practice that I should go back through all of my headers and fix (it's a cross

Boolean multiplication in c++?

让人想犯罪 __ 提交于 2019-12-23 07:00:39
问题 Consider the following: inline unsigned int f1(const unsigned int i, const bool b) {return b ? i : 0;} inline unsigned int f2(const unsigned int i, const bool b) {return b*i;} The syntax of f2 is more compact, but do the standard guarantees that f1 and f2 are strictly equivalent ? Furthermore, if I want the compiler to optimize this expression if b and i are known at compile-time, which version should I prefer ? 回答1: Well, yes, both are equivalent. bool is an integral type and true is

Does casting away constness from “this” and then changing a member value invoke undefined behaviour?

依然范特西╮ 提交于 2019-12-22 05:10:39
问题 In a response to my comment to some answer in another question somebody suggests that something like void C::f() const { const_cast<C *>( this )->m_x = 1; } invokes undefined behaviour since a const object is modified. Is this true? If it isn't, please quote the C++ standard (please mention which standard you quote from) which permits this. For what it's worth, I've always used this approach to avoid making a member variable mutable if just one or two methods need to write to it (since using