c++11

std::is_same equivalent for unspecialised template types

你离开我真会死。 提交于 2021-02-09 00:25:17
问题 In one project I have found a possibility to stay DRY as a lot of code except for some small parts could stay the same for template specialisations of a template. Here is a small working example what I'm currently doing to check which templated class I'm using: template<typename T> class A{}; template<typename T> class B{}; template<template<class> class C> void do_stuff() { if(std::is_same<A<int>,C<int>>::value) { // Do Stuff for A } else if(std::is_same<B<int>,C<int>>::value) // Do Stuff

std::is_same equivalent for unspecialised template types

醉酒当歌 提交于 2021-02-09 00:22:32
问题 In one project I have found a possibility to stay DRY as a lot of code except for some small parts could stay the same for template specialisations of a template. Here is a small working example what I'm currently doing to check which templated class I'm using: template<typename T> class A{}; template<typename T> class B{}; template<template<class> class C> void do_stuff() { if(std::is_same<A<int>,C<int>>::value) { // Do Stuff for A } else if(std::is_same<B<int>,C<int>>::value) // Do Stuff

C++11 atomics: does it make sense, or is it even possible, to use them with memory mapped I/O?

坚强是说给别人听的谎言 提交于 2021-02-08 23:32:51
问题 As I understand it, C volatile and optionally inline asm for memory fence have been used for implementing a device driver on top of memory mapped I/O. Several examples can be found in Linux kernel. If we forget about the risk of uncaught exceptions (if any,) does it make sense to replace them with C++11 atomics? Or, is it possible at all? 回答1: In general, you can replace memory fences with atomics, but not volatile , except where it is used together with a fence exclusively for inter thread

Default value of a struct member

三世轮回 提交于 2021-02-08 21:41:49
问题 (I'm sure this question has already been answered, I'm just not sure on the right words to use to ask it. If someone would tell me what the correct terminology is that would be awesome!) I'm implementing a HashSet in C++ for a data structures class, and I have a question about C++ syntax regarding structs. Here is my code: struct HashNode { T value; HashNode* next = nullptr; }; Will this code correctly initialize the next pointer to nullptr when new HashNode is called? If not, what is the

C++11: is std::locale::empty() even a thing?

给你一囗甜甜゛ 提交于 2021-02-08 16:54:45
问题 Trying to compile some code from StackOverflow, basically, these lines: std::wifstream wif(filename); wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>)); GCC version: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 I get a compile error: 'empty' is not a member of 'std::locale' And I agree with the compiler, checked with documentation like cppreference - there is no info about such thing. Header files as well do not show anything. I wonder, if this is just me or the sample

extern template & incomplete types

a 夏天 提交于 2021-02-08 14:19:27
问题 Recently when I was trying to optimize my include hierarchy I stumbled upon the file a.hpp : template<class T> class A { using t = typename T::a_t; }; class B; extern template class A<B>; which seems to be ill-formed. In fact it seems as if the extern template statement at the end causes an instantiation of A<B> which causes the compiler to complain about an incomplete type. My goal would have been to define A<B> in a.cpp : #include <b.hpp> template class A<B>; This way I avoid having to

What if an object passed into std::swap throws an exception during swapping?

◇◆丶佛笑我妖孽 提交于 2021-02-08 14:16:04
问题 The C++ standard guarantees that std::swap will throw no exception. However, what if an object to swap throws an exception during swapping? Next, how should the caller find an exception has happened? and what measures should the caller take? PS: It is very common that a constructor throws an exception. struct A { A(const A&) { throw 1; } A& operator =(const A&) { throw 2; return *this; } }; int main() { A a1, a2; std::swap(a1, a2); // An exception happened, but the caller doesn't know. // How

What if an object passed into std::swap throws an exception during swapping?

 ̄綄美尐妖づ 提交于 2021-02-08 14:15:41
问题 The C++ standard guarantees that std::swap will throw no exception. However, what if an object to swap throws an exception during swapping? Next, how should the caller find an exception has happened? and what measures should the caller take? PS: It is very common that a constructor throws an exception. struct A { A(const A&) { throw 1; } A& operator =(const A&) { throw 2; return *this; } }; int main() { A a1, a2; std::swap(a1, a2); // An exception happened, but the caller doesn't know. // How

What if an object passed into std::swap throws an exception during swapping?

痞子三分冷 提交于 2021-02-08 14:14:44
问题 The C++ standard guarantees that std::swap will throw no exception. However, what if an object to swap throws an exception during swapping? Next, how should the caller find an exception has happened? and what measures should the caller take? PS: It is very common that a constructor throws an exception. struct A { A(const A&) { throw 1; } A& operator =(const A&) { throw 2; return *this; } }; int main() { A a1, a2; std::swap(a1, a2); // An exception happened, but the caller doesn't know. // How

Mixing pass-by-reference and pass-by-value to variadic template function valid?

此生再无相见时 提交于 2021-02-08 14:11:27
问题 I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } Is it valid using this function to mix pass-by-value and pass-by-reference? For example allocating a class with a constructor with some by-value and some by-reference. It compiles, but I'm not sure if it has any nasty side-effects or not. 回答1: What