c++14

Defining interface of abstract class in shared library

允我心安 提交于 2020-01-24 17:23:36
问题 Say I have a abstract base class defined like so: interface.hpp #ifndef INTERFACE_HPP #define INTERFACE_HPP 1 class interface{ public: virtual void func() = 0; }; #endif // INTERFACE_HPP Then I compile a translation unit test.cpp into a shared object test.so : test.cpp #include "interface.hpp" #include <iostream> class test_interface: public interface{ public: void func(){std::cout << "test_interface::func() called\n";} }; extern "C" interface &get_interface(){ static test_interface test;

Template argument and deduction of std::function parameters

廉价感情. 提交于 2020-01-24 12:07:38
问题 Assume there is a template function foo() which accepts an arbitrary number of arguments. Given the last argument is always an std::function , how do I implement a foo() template shown below in a way that CbArgs would contain this std::function 's parameters? template<typename... InArgs, typename... CbArgs = ???> // ^^^^^^^^^^^^ void foo(InArgs... args) { ... } For example, CbArgs should be {int,int} if invoked like this: std::function<void(int,int)> cb; foo(5, "hello", cb); My first idea was

meta object using “static virtual” functions

时光总嘲笑我的痴心妄想 提交于 2020-01-24 09:25:05
问题 I developed some kind of meta object mechanism in a project, in order to associate type names and properties of any type to an object (see the result here). I have a dirty code working and I'm trying to make it cleaner. Given the following dummy structures: struct A { using Self = A; using Base = void; static std::string get_type_name(){ return { "A" }; } static std::vector<int> get_properties(){ return { 0 }; } }; #define SELF(class_name)\ using Base = Self;\ using Self = class_name; struct

c++ reversed integer sequence implementation

ぐ巨炮叔叔 提交于 2020-01-23 12:07:54
问题 Who knows how to implement C++ std::make_index_sequence reverse version. To get - make_index_sequence_reverse<int, 5> = <4,3,2,1,0> . Thank you! 回答1: IMHO, there is no reason for a index_sequence_reverse : std::index_sequence support sequences of indexes and are order neutral (or even without order). If you can use std::make_index_sequence , for a makeIndexSequenceReverse you can make something as follows #include <utility> #include <type_traits> template <std::size_t ... Is> constexpr auto

c++ reversed integer sequence implementation

点点圈 提交于 2020-01-23 12:07:40
问题 Who knows how to implement C++ std::make_index_sequence reverse version. To get - make_index_sequence_reverse<int, 5> = <4,3,2,1,0> . Thank you! 回答1: IMHO, there is no reason for a index_sequence_reverse : std::index_sequence support sequences of indexes and are order neutral (or even without order). If you can use std::make_index_sequence , for a makeIndexSequenceReverse you can make something as follows #include <utility> #include <type_traits> template <std::size_t ... Is> constexpr auto

auto, decltype(auto) and trailing return type

风格不统一 提交于 2020-01-23 11:06:32
问题 Is there a difference between: template <class T> constexpr decltype(auto) f(T&& x) -> decltype(std::get<0>(std::forward<T>(x))) { return std::get<0>(std::forward<T>(x)); } and: template <class T> constexpr auto f(T&& x) -> decltype(std::get<0>(std::forward<T>(x))) { return std::get<0>(std::forward<T>(x)); } and if so, what is it, and which one should I use for perfect forwarding? 回答1: Trailing return type should only be used with auto The point of decltype(auto) vs auto is to distinguish the

Is it valid to do explicit template specialisation with auto return 'type' in C++14?

倖福魔咒の 提交于 2020-01-23 07:26:13
问题 Previous question. I repeat the code from the previous question to make this question self-contained. The code below compiles and does not issue any warnings if it is compiled using gcc 4.8.3. with -std=c++1y . However, it does issue warnings if compiled with -std=c++0x flag. In the context of the previous question it was stated that the code does not compile using gcc 4.9.0. Unfortunately, at present, I do not fully understand how auto is implemented. Thus, I would appreciate if anyone could

compile time typeid for every type

会有一股神秘感。 提交于 2020-01-23 07:20:32
问题 I'd like a constexpr function that will return me a unique id for every C++ type, something like this: using typeid_t = uintptr_t; template <typename T> constexpr typeid_t type_id() noexcept { return typeid_t(type_id<T>); } int main() { ::std::cout << ::std::integral_constant<typeid_t, type_id<float>()>{} << ::std::endl; return 0; } But I get an error: t.cpp: In function 'int main()': t.cpp:23:69: error: conversion from pointer type 'typeid_t (*)() noexcept {aka long unsigned int (*)()

A way to filter range by indices, to get min_element from filtered indices only?

ぃ、小莉子 提交于 2020-01-23 06:12:18
问题 In comments to this question is-there-a-way-to-iterate-over-at-most-n-elements-using-range-based-for-loop there was additional question - is this possible to have "index view" on a container, i.e. to have subrange with some indexes filtered out. Additionally I encountered a problem to find minimum value from a range with some indexes filtered out. I.e. is it possible to replace such code as below with std and/or boost algorithms, filters - to make it more readable and maintainable: template

T declval() instead of T && declval() for common_type

半世苍凉 提交于 2020-01-22 15:33:05
问题 Isn't it better to use std::declval declared in form: template< class T > T declval(); // (1) then current one: template< class T > T && declval(); // (2) for std::common_type (possibly with different name only for this current purpose)? Behaviour of common_type using (1) is closer to the behaviour of the ternary operator (but not using std::decay_t ) than the behaviour when using (2) : template< typename T > T declval(); template <class ...T> struct common_type; template< class... T > using