c++03

In which versions of the C++ standard does “(i+=10)+=10” have undefined behaviour?

前提是你 提交于 2019-12-17 10:53:20
问题 In C++, does the following have undefined behaviour: int i = 0; (i+=10)+=10; There was some debate about this in the comments to my answer to What's the result of += in C and C++? The subtlety here is that the default response seems to be "yes", whereas it appears that the correct answer is "it depends on the version of the C++ standard". If it does depend on the version of the standard, please explain where it's UB and where it's not. 回答1: tl;dr : The sequence of the modifications and reads

Purpose of Trigraph sequences in C++?

只谈情不闲聊 提交于 2019-12-17 04:14:44
问题 According to C++'03 Standard 2.3/1: Before any other processing takes place, each occurrence of one of the following sequences of three characters (“trigraph sequences”) is replaced by the single character indicated in Table 1. ---------------------------------------------------------------------------- | trigraph | replacement | trigraph | replacement | trigraph | replacement | ---------------------------------------------------------------------------- | ??= | # | ??( | [ | ??< | { | | ??/

initialize a const array in a class initializer in C++

自古美人都是妖i 提交于 2019-12-17 02:13:12
问题 I have the following class in C++: class a { const int b[2]; // other stuff follows // and here's the constructor a(void); } The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const ? This doesn't work: a::a(void) : b([2,3]) { // other initialization stuff } Edit: The case in point is when I can have different values for b for different instances, but the values are known to be

Eliminate redundancy with CRTP and multiple inheritance

て烟熏妆下的殇ゞ 提交于 2019-12-14 03:57:31
问题 This question is for C++03, not C++11. I have a case where I am using CRTP with multiple inheritance, and I am curious to know if there is a way to remove the redundancy that is created when specifying the type of B below. #include "boost/typeof/typeof.hpp" #include "boost/units/detail/utility.hpp" #include <iostream> #include <string> struct One{}; struct Two{}; template<typename T> struct Type { static std::string name(void) { return boost::units::detail::demangle(typeid(T).name()); } };

g++ error: expected primary-expression

北战南征 提交于 2019-12-13 21:22:21
问题 look at this sample: struct parent { template <typename t> inline static t get_t(); }; struct child : public parent { template <typename t> inline static t get_t() { return t(-1); } }; template <typename t> struct call { inline static int get_value() { return t::get_t<int>(); } }; typedef call< child > test; int main() { int v = test::get_value(); } The code compile with the following error: In static member function 'static int call<t>::get_value()': error: expected primary-expression before

How to implement fill constructor and range constructor for sequence containers unambiguously

拜拜、爱过 提交于 2019-12-13 04:39:26
问题 Sequence containers need to have fill constructors and range constructors, i.e. these must both work, assuming MyContainer models a sequence container whose value_type is int and size_type is std::size_t : // (1) Constructs a MyContainer containing the number '42' 4 times. MyContainer<int> c = MyContainer<int>(4, 42); // (2) Constructs a MyContainer containing the elements in the range (array.begin(), array.end()) std::array<int, 4> array = {1, 2, 3, 4}; MyContainer<int> c2 = MyContainer<int>

MATLAB R2016b - MEX fails to compile C++ code

不想你离开。 提交于 2019-12-13 03:08:03
问题 My last post was cluttered with lots of information. Part of it was this problem, which hopefully on its own with more info will make more sense. I am attempting to use mex in MATLAB R2016b to compile C++03 code on Windows. When I try to do so, I get the following errors: Error using mex cpp_mexapi_version.o: In function `mexfilerequiredapiversion': C:/Progra~1/MATLAB/R2016b/extern/version/cpp_mexapi_version.cpp:4: multiple definition of `mexfilerequiredapiversion' C:\Users\myName\AppData

Can I persude GCC to inline a deferred call through a stored function pointer?

纵然是瞬间 提交于 2019-12-12 14:05:28
问题 Naturally, C++ compilers can inline function calls made from within a function template, when the inner function call is directly known in that scope (ref). #include <iostream> void holyheck() { std::cout << "!\n"; } template <typename F> void bar(F foo) { foo(); } int main() { bar(holyheck); } Now what if I'm passing holyheck into a class, which stores the function pointer (or equivalent) and later invokes it? Do I have any hope of getting this inlined? How? template <typename F> struct Foo

When should I use references in C++?

↘锁芯ラ 提交于 2019-12-12 12:12:10
问题 I've been programming C++ for a while now and I'm starting to doubt that the rule use references whenever possible should be applied everywhere. Unlike this related SO post I'm interested in a different kind of thing. In my experience the reference/pointer mix messes up your code: std::vector<Foo *> &x = get_from_somewhere(); // OK? reference as return value some_func_pass_by_ref(x); // OK reference argument and reference variable some_func_by_pointer(x[4]); // OK pointer arg, pointer value

How to stable_sort without copying?

孤街浪徒 提交于 2019-12-12 11:29:49
问题 Why does stable_sort need a copy constructor? ( swap should suffice, right?) Or rather, how do I stable_sort a range without copying any elements? #include <algorithm> class Person { Person(Person const &); // Disable copying public: Person() : age(0) { } int age; void swap(Person &other) { using std::swap; swap(this->age, other.age); } friend void swap(Person &a, Person &b) { a.swap(b); } bool operator <(Person const &other) const { return this->age < other.age; } }; int main() { static size