language-lawyer

C++11 interface pure virtual destructor

心已入冬 提交于 2019-12-21 15:18:14
问题 UPD . There is a mark that it is a duplicate of this question. But in that question OP asks HOW to use default to define pure virtual destructor. This question is about what the difference . In C++ (latest standard if possible) what the real difference between defining pure virtual destructor with empty body implementation and just a empty body (or default)? Variant 1: class I1 { public: virtual ~I1() {} }; Variant 2.1: class I21 { public: virtual ~I21() = 0; }; I21::~I21() {} Variant 2.2:

How does an unspecified pointer conversion behave in C++14?

跟風遠走 提交于 2019-12-21 13:05:19
问题 The result of some pointer casts are described as unspecified. For example, [expr.static.cast]/13: A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” [...] If the original pointer value represents the address A of a byte in memory and A satisfies the alignment requirement of T, then the resulting pointer value represents the same address as the original pointer value, that is, A. The result of any other such pointer conversion is unspecified . My

Specialization of inherited nested template class

房东的猫 提交于 2019-12-21 12:56:31
问题 The following source code is brought from: Understanding partial specialization of inherited nested class templates #include <type_traits> struct Base { template<class U, class _ = void> struct Inner: std::true_type {}; template<class _> struct Inner<char, _>: std::false_type {}; }; struct Derived : Base { }; template<class _> struct Derived::Inner<int, _>: std::false_type {}; I had an issue about specializing inherited class, so I googled, and find out the question above. The source code in

Is it safe to cast a heap allocated pointer to a pointer to a VLA?

不羁岁月 提交于 2019-12-21 12:23:25
问题 If I've got a pointer to some heap allocated space that represents a typical row-major two dimensional array, is it safe to cast this pointer to an equivalent pointer to a VLA for convenient sub-scripting? Example: // // Assuming 'm' was allocated and initialized something like: // // int *matrix = malloc(sizeof(*matrix) * rows * cols); // // for (int r = 0; r < rows; r++) { // for (int c = 0; c < cols; c++) { // matrix[r * cols + c] = some_value; // } // } // // Is it safe for this function

std::launder and strict aliasing rule

倖福魔咒の 提交于 2019-12-21 12:19:46
问题 Consider this code: void f(char * ptr) { auto int_ptr = reinterpret_cast<int*>(ptr); // <---- line of interest // use int_ptr ... } void example_1() { int i = 10; f(reinterpret_cast<char*>(&i)); } void example_2() { alignas(alignof(int)) char storage[sizeof(int)]; new (&storage) int; f(storage); } line of interest with call from example_1: Q1: On the callside the char pointer is aliasing our integer pointer. This is valid. But is it also valid to just cast it back to an int? We know an int is

Different behavior among explicit cast, direct initialization and copy initialization

大兔子大兔子 提交于 2019-12-21 12:15:03
问题 I have a class C which has a casting operator to anything. In the example I tried to cast an instance of it to std::string in three different ways: static_cast , constructor of std::string and assigning to std::string . However, only the last one compiles, while the others raise an error of ambiguous constructor. The reason of the error is clear enough: there are many ways to convert C to something which the constructor of std::string can accept. But what is the difference between these cases

Is template-name<TT> a deduced context?

寵の児 提交于 2019-12-21 11:56:09
问题 [temp.deduct.type] paragraph 8 lists all deduced contexts, but it seems not to include template-name <TT> where template-name refers to a class template and TT refers to a template template argument. Is this a deduced context? If it is, why? If not, consider the following code: template<template<typename> class U, template<typename> class V> struct foo {}; template<template<typename> class U> struct foo<U, U> {}; int main() {} This code compiles under Clang 7.0.0 and GCC 8.0.1, which means

Is template-name<TT> a deduced context?

两盒软妹~` 提交于 2019-12-21 11:55:16
问题 [temp.deduct.type] paragraph 8 lists all deduced contexts, but it seems not to include template-name <TT> where template-name refers to a class template and TT refers to a template template argument. Is this a deduced context? If it is, why? If not, consider the following code: template<template<typename> class U, template<typename> class V> struct foo {}; template<template<typename> class U> struct foo<U, U> {}; int main() {} This code compiles under Clang 7.0.0 and GCC 8.0.1, which means

Formal specification of std::vector<T>::pop_back

不想你离开。 提交于 2019-12-21 10:48:05
问题 I'm looking in the C++ Standard (draft n3797), and I can't find any documentation of pop_back as it applies to std::vector , only for std::list . Is it really missing? Specifically I was looking for the guarantee that pop_back doesn't change the capacity. Or is there such a guarantee at all? (I expect that iterators and references to other elements will remain valid, but I can't find that guarantee, and it wouldn't restrict the case of removing the last element, anyway) 回答1: No it doesn't

Formal specification of std::vector<T>::pop_back

时光总嘲笑我的痴心妄想 提交于 2019-12-21 10:47:30
问题 I'm looking in the C++ Standard (draft n3797), and I can't find any documentation of pop_back as it applies to std::vector , only for std::list . Is it really missing? Specifically I was looking for the guarantee that pop_back doesn't change the capacity. Or is there such a guarantee at all? (I expect that iterators and references to other elements will remain valid, but I can't find that guarantee, and it wouldn't restrict the case of removing the last element, anyway) 回答1: No it doesn't