c++14

DLL exporting causing issues with unique pointers

走远了吗. 提交于 2021-02-05 06:25:05
问题 I've got two files: Header.h #pragma once #ifdef UNIQUEPTRISSUE_EXPORTS #define UNIQUEPTRISSUE_API __declspec(dllexport) #else #define UNIQUEPTRISSUE_API __declspec(dllimport) #endif UniquePtrIssue.cpp #include "stdafx.h" #include "Header.h" #include <memory> #include <vector> class UNIQUEPTRISSUE_API ClassA { }; class UNIQUEPTRISSUE_API ClassB { private: std::vector<std::unique_ptr<ClassA>> x; }; Compiling raises the following error: 1>d:\program files (x86)\microsoft visual studio\2017

User-defined infix operators

南楼画角 提交于 2021-02-04 15:09:25
问题 It is easy to introduce new infix operators in C++ // User-defined infix operator framework template <typename LeftOperand, typename Operation> struct LeftHelper { const LeftOperand& leftOperand; const Operation& operation; LeftHelper(const LeftOperand& leftOperand, const Operation& operation) : leftOperand(leftOperand), operation(operation) {} }; template <typename LeftOperand, typename Operation > auto operator < (const LeftOperand& leftOperand, Operation& operation) { return LeftHelper

Recursively unpacking a template pack for a parameter-less function

纵然是瞬间 提交于 2021-01-29 13:36:09
问题 I'm trying to create a struct template with a variadic template type pack, that can deduct the sum of the size of all types passed in. Below you find a simplified example, in the real-world context, the size computed is used to create further member objects. template <typename... Types> struct OverallSize { template <typename FirstType, typename... NextTypes> static constexpr size_t sizesum() { return sizeof (FirstType) + sizesum<NextTypes...>(); } template <typename LastType> static

Check if template type is any instantiation of std::array [duplicate]

扶醉桌前 提交于 2021-01-29 12:08:12
问题 This question already has an answer here : C++11 is_same type trait for templates (1 answer) Closed 10 months ago . I'm in need of some compile time check if a template type passed into a templated function is any instantiation of std::array Like IsStdArray<std::array<float, 12>>::value; // should evaluate to true IsStdArray<std::array<int, 1000>>::value; // should evaluate to true IsStdArray<std::vector<float>>::value; // should evaluate to false IsStdArray<std::string>::value // should

Issue with X3 and MS VS2017

三世轮回 提交于 2021-01-29 10:30:46
问题 I am having a weird problem with boost spirit X3 (v1.69) in combination with MS VS2017. I am getting compiling errors in well formed structures. When I use the same code block in gcc and clang through Coliru or Wandbox, the source compiles and everything goes right. But when I use that same code in VS 2017, compiling errors appears until I comment an 'omit' sentence. Please, any help with this? #include <vector> #include <iostream> #include <boost/spirit/home/x3.hpp> #include <boost/fusion

Function to calculate how often users are pressing a button

三世轮回 提交于 2021-01-29 08:59:07
问题 I have got a function. Below is a prototype void onNewButtonPress(int64_t nanoseconds_timestamp, int32_t user_id); A bit of destription. This function will be called each time a user with a user_id is pressing the button. Where nanoseconds_timestamp parameter is the time in nanoseconds since the epoch This function will need to get the rate of user button presses, basically how many times per second a user pressed a button. How can I calculate the rate for each user, store it and update it

Compile error with boost::spirit::x3

拟墨画扇 提交于 2021-01-29 05:14:32
问题 I tried to compile example file with gcc 5.3.1 ( 5.3.1 20160406 (Red Hat 5.3.1-6) (GCC) ), boost 1.61.0. #include <boost/config/warning_disable.hpp> #include <boost/spirit/home/x3.hpp> #include <iostream> // Presented are various ways to attach semantic actions // * Using plain function pointer // * Using simple function object namespace client { namespace x3 = boost::spirit::x3; using x3::_attr; struct print_action { template <typename Context> void operator()(Context const& ctx) const { std

Allow a mock class to inherit from a final class

牧云@^-^@ 提交于 2021-01-29 02:50:47
问题 We may declare a final/sealed non-inheritable class using the new C++ keyword final . class Generator final { }; This class may inherit from other, may or may not have virtual (inherited or not). But, how to make it final , yet allow one class to inherit from it? We mostly need to derive a mock class from real class (with or without late-binding, hence virtual isn't important). How to make it work: class MockGenerator : Generator{}; But disallow any other inheritance? 回答1: But, how to make it

Template member function in a template class not found when called on an instance

此生再无相见时 提交于 2021-01-28 19:01:55
问题 namespace { enum class API { CPU, CUDA }; template <API Api> class Allocator { void* malloc(int size) const; void free(void* ptr) const; template <typename T> T* alloc1(int num) const { return static_cast<T*>(malloc(num * static_cast<int>(sizeof(T)))); } }; template <typename T, API Api> T* alloc2(const Allocator<Api> allocator, int num) { return static_cast<T*>(allocator.malloc(num * static_cast<int>(sizeof(T)))); } template <> class Allocator<API::CPU> { public: void* malloc(int size) const

std::unique_ptr with std::map

谁都会走 提交于 2021-01-28 07:27:25
问题 I have a std::map where the key is std::shared_ptr<Foo> and the value is std::unique_ptr<Bar> where Foo and Bar are very different classes from a third-party library. I am using this std::map object as an in-memory cache. I am wondering what the best way of inserting a new entry into this map will be and then returned from a method, given that the Bar passed into the std::unique_ptr will already be constructed? I currently have the following: class SomeClass { public: const Bar*