c++11

How to remove last argument of variadic template

北城余情 提交于 2021-02-19 02:28:06
问题 I have following structure, I want remove last argument from index_sequence : template< std::size_t ... values> struct index_sequence{}; // I need something like template< typename IndexSequence> struct pop_back; template< std::size_t ... values > struct pop_back< index_sequence< values... > > { typedef index_sequence< /** values except last one*/ > type; }; How to implement this pop_back structure? I know implementation, only it requires deep recursion, I want without deep recursion

Is it safe to modify mutable members of objects inside sets?

允我心安 提交于 2021-02-19 00:39:12
问题 I was curious as to whether the following scenario is safe. I have the following class definitions: class ActiveStatusEffect { public: StatusEffect* effect; mutable int ReminaingTurns; ActiveStatusEffect() : ReminaingTurns(0) { } //Other unimportant stuff down here } I then store a group of these inside an std::set as follows: struct ASECmp { bool operator ()(const StatusEffects::ActiveStatusEffect &eff1, const StatusEffects::ActiveStatusEffect &eff2) { return eff1.effect->GetPriority() <

Eigen::aligned_allocator fails with std::unordered_multimap

烂漫一生 提交于 2021-02-19 00:37:55
问题 I am trying to compile this code in XCode 6: std::unordered_multimap< Frame*, Sim3, std::hash<Frame*>, std::equal_to<Frame*>, Eigen::aligned_allocator< std::pair<const Frame*,Sim3> > > trackingFailed; It fails with: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:1461:5: Static_assert failed "Invalid allocator::value_type" Is it still necessary to use aligned_allocator in Eigen 3.2.2? Why is it failing with C++11/C++14 and libc++

Compile-time template `std::integral_constant` counter - how to implement it?

守給你的承諾、 提交于 2021-02-18 23:00:47
问题 I have several types and I want to "bind" an std::integral_constant sequential ID value to every type at compile-time. Example: struct Type00 { }; struct Type01 { }; struct Type02 { }; struct Type03 { }; struct TypeXX { }; struct TypeYY { }; template<typename T> struct TypeInfo { using Id = std::integral_constant<int, ???>; }; int main() { cout << TypeInfo<Type00>::Id::value; // Should always print 0 cout << TypeInfo<Type01>::Id::value; // Should always print 1 cout << TypeInfo<Type02>::Id:

using typedef in template instantiation and extern template declaration

穿精又带淫゛_ 提交于 2021-02-18 22:55:19
问题 There are two cases where typedef confuses me when it comes to extern template declaration and explicit template instantiation . To illustrate the two see below 2 example code snippets. Consider following example (Case 1) : // suppose following code in some cpp file template <typename T> struct example { T value; }; // valid typedefs typedef example<int> int_example; typedef example<std::string> string_example; // explicit instantiation using above typedefs template class int_example; // ->

override map::compare with lambda function directly

假如想象 提交于 2021-02-18 20:01:26
问题 Trying to override map::compare function using lambda, it seems that the following solution works. auto cmp = [](const int&a, const int& b) { return a < b; }; std::map<int, int, decltype(cmp)> myMap(cmp); But, I had to define cmp first and use it later. Can I do this without defining 'cmp'? 回答1: No, you can't use lambda in unevaluated context -- i.e. template parameters as in your example. So you must define it somewhere else (using auto ) and then use decltype ... the other way, as it was

C++11 cin input validation

拥有回忆 提交于 2021-02-18 19:09:39
问题 What is the best way in C++11 (ie. using C++11 techniques) to validate cin input? I've read lots of other answers (all involving cin.ignore, cin.clear, etc.), but those methods seem clumsy and result in lots of duplicated code. Edit: By 'validation', I mean that both well-formed input was provided, and that it satisfies some context-specific predicate. 回答1: I'm posting my attempt at a solution as an answer in the hopes that it is useful to somebody else. It is not necessary to specify a

Portable C++ Singleton - When is the destructor called

三世轮回 提交于 2021-02-18 17:45:50
问题 When a thread safe singleton has to be implemented using C++11 the only correct implementation I know is the following: // header class Singleton final { public: static Singleton& getInstance(); private: Singleton() = default; Singleton(Singleton const&) = delete; void operator=(Singleton const&) = delete; }; // implementation: Singleton& Singleton::getInstance() { static Singleton instance; return instance; } In his Book "C++ Concurrency in Action" A. Williams writes that since C++11 "the

Error using boost serialization with binary archive

南笙酒味 提交于 2021-02-18 17:12:49
问题 I get the following error while reading from boost::archive::binary_iarchive into my variable: test-serialization(9285,0x11c62fdc0) malloc: can't allocate region *** mach_vm_map(size=18014398509486080) failed (error code=3) test-serialization(9285,0x11c62fdc0) malloc: *** set a breakpoint in malloc_error_break to debug My serialization and deserialization code are: template<class Archive> void save(Archive & archive, const helib::PubKey & pubkey, const unsigned int version){ BOOST_TEST

Error using boost serialization with binary archive

别来无恙 提交于 2021-02-18 17:12:12
问题 I get the following error while reading from boost::archive::binary_iarchive into my variable: test-serialization(9285,0x11c62fdc0) malloc: can't allocate region *** mach_vm_map(size=18014398509486080) failed (error code=3) test-serialization(9285,0x11c62fdc0) malloc: *** set a breakpoint in malloc_error_break to debug My serialization and deserialization code are: template<class Archive> void save(Archive & archive, const helib::PubKey & pubkey, const unsigned int version){ BOOST_TEST