language-lawyer

Static templated constexpr nested class member

☆樱花仙子☆ 提交于 2019-12-20 02:15:47
问题 I have the following sample class Foo with nested class Bar and everything is constexpr : class Foo { private: template <typename T> struct Bar { constexpr Bar(){} constexpr int DoTheThing() const { return 1; } }; public: constexpr static auto b = Bar<int>{}; constexpr Foo() {} constexpr int DoTheThing() const { return b.DoTheThing(); } }; And I want to test that calling Foo::DoTheThing returns 1: int main() { constexpr Foo f; static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1")

When can `typename` be used with identifiers that unambiguously refer to a type?

六月ゝ 毕业季﹏ 提交于 2019-12-20 02:15:20
问题 Normally, typename is used to disambiguate between cases where an identifier may refer to a type, or may refer to something else: template<class T> void foo(typename T::type value) { // ... } When can typename be used when an identifier is already a type? 1. Can it be used if there's already a class with this name? class MyClass{}; void foo(typename MyClass value) {} 2. Can it be used with a template parameter that's declared as a type? template<class T> void foo(typename T value) {} 3. Can

Which operations are defined for invalid iterators?

◇◆丶佛笑我妖孽 提交于 2019-12-20 02:14:03
问题 As a follow-up on a question concerning comparing invalid iterators, I tried to find a definition of the allowed expressions for invalid iterators in the C++ standard. Searching for "invalid iterator" finds only a single reference in §24.2.1.11. It says that invalid iterators may be "singular", but only states that dereferencing them may be undefined behavior. No further semantics is given. One of the original answers suggests that it is implementation-defined behavior, but I think that this

Cannot access protected member of another instance from derived type's scope

橙三吉。 提交于 2019-12-20 02:12:41
问题 In this answer to the question " Why can't my object access protected members of another object defined in common base class? ", one can read: You can only access protected members from your own base class instance. Either I don't get it correctly or the following MCVE (live on coliru) proves it wrong: struct Base { void f(); protected: int prot; }; struct Derived : Base { void g(); private: int priv; }; void Base::f() { Base b; b.prot = prot; (void) b; } void Derived::g() { { Derived d;

Is a vector with incomplete type allowed if absolutely no member functions are called? If so, since when?

好久不见. 提交于 2019-12-20 01:08:21
问题 Suppose I have some incomplete type // in foo.hh struct Hidden; that I want to use as element type of a std::vector . Using an union I can "defer" calls to the constructor(s) and the destructor of the std::vector to the implementation of the unions constructor(s) / destructor. // in foo.hh struct Public { union Defer { std::vector<Hidden> v; Defer(); // add copy/move constructor if needed ~Defer(); } d; }; Now I can use Public by only including foo.hh and linking with the file(s) implementing

how to understand volatile example in Java Language Specification?

蹲街弑〆低调 提交于 2019-12-20 01:06:02
问题 I think example of volatile in Java specification is a little wrong. In 8.3.1.4. volatile Fields, it says class Test { static int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } } ...then method two could occasionally print a value for j that is greater than the value of i, because the example includes no synchronization and, under the rules explained in§17.4, the shared values of i and j might be updated out of order. I think even

cin overwriting my initialized value when it reads wrong type? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-19 22:33:31
问题 This question already has answers here : Why does stringstream >> change value of target on failure? (2 answers) Closed 2 years ago . So this is a really basic question and super trivial but Im just going through programming principles & practices in c++ and my program for reading in a string and a int is behaving differently than the book which is written by Bjarne Stroustrup so id be surprised if he made a mistake. Anyway here's the code: #include "..\std_lib_facilities.h" int main() { cout

C++ Unexpected Integer Promotion

筅森魡賤 提交于 2019-12-19 17:46:58
问题 I was writing some code recently that was actually supposed to test other code, and I stumbled upon a surprising case of integer promotion. Here's the minimal testcase: #include <cstdint> #include <limits> int main() { std::uint8_t a, b; a = std::numeric_limits<std::uint8_t>::max(); b = a; a = a + 1; if (a != b + 1) return 1; else return 0; } Surprisingly this program returns 1. Some debugging and a hunch revealed that b + 1 in the conditional was actually returning 256, while a + 1 in

Using a constexpr static member of a reference as template argument

让人想犯罪 __ 提交于 2019-12-19 17:41:51
问题 I'm trying to figure out whether GCC or Clang interpret the C++17 standard differently / wrong here. This is my code, which does compile using GCC 8, but not using Clang 6: struct BoolHolder { constexpr static bool b = true; }; template<bool b> class Foo {}; int main() { BoolHolder b; Foo<b.b> f; // Works BoolHolder & br = b; Foo<br.b> f2; // Doesn't work } I wonder why that is. Obviously, b.b is a valid constexpr (or the first Foo<b.b> wouldn't be valid). Is br.b not a valid constexpr? Why?

Using a constexpr static member of a reference as template argument

雨燕双飞 提交于 2019-12-19 17:41:10
问题 I'm trying to figure out whether GCC or Clang interpret the C++17 standard differently / wrong here. This is my code, which does compile using GCC 8, but not using Clang 6: struct BoolHolder { constexpr static bool b = true; }; template<bool b> class Foo {}; int main() { BoolHolder b; Foo<b.b> f; // Works BoolHolder & br = b; Foo<br.b> f2; // Doesn't work } I wonder why that is. Obviously, b.b is a valid constexpr (or the first Foo<b.b> wouldn't be valid). Is br.b not a valid constexpr? Why?