c++14

Why specializing a type_trait could result in undefined behaviour?

♀尐吖头ヾ 提交于 2019-12-13 11:43:43
问题 Discussion According to the standard §20.10.2/1 Header <type_traits> synopsis [meta.type.synop]: 1 The behavior of a program that adds specializations for any of the class templates defined in this subclause is undefined unless otherwise specified. This specific clause contradicts to the general notion that STL should be expandible and prevents us from expanding type traits as in the example below: namespace std { template< class T > struct is_floating_point<std::complex<T>> : std::integral

Why does the addition of two shorts return an int? [closed]

喜你入骨 提交于 2019-12-13 10:13:19
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . I read a c++ tutorial that says arithmetic operators return the smallest data type possible (i.e. if 2 ints are added the return type will be int, if a float and a double are added the return type will be double). However, it also said that arithmetic operations on shorts return ints. Considering

How to specify type of a constexpr function returning a class (without resorting to auto keyword)

a 夏天 提交于 2019-12-13 09:56:06
问题 Basically in below I want to see if I can get around having to use auto keyword Suppose that we have the following piece of code [works with g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) & clang version 3.6.0] : //g++ -std=c++14 test.cpp //test.cpp #include <iostream> using namespace std; template<typename T> constexpr auto create() { class test { public: int i; virtual int get(){ return 123; } } r; return r; } auto v = create<int>(); int main(void){ cout<<v.get()<<endl; } How can I specify the type of

Passing a variadic function as argument

点点圈 提交于 2019-12-13 07:19:54
问题 Consider this working code: #include <iostream> #include <utility> #include <array> template <typename... Args> void foo (Args&&... args) { const auto v = {args...}; for (auto x : v) std::cout << x << ' '; std::cout << '\n'; } template <typename> struct Foo; template <std::size_t... Is> struct Foo<std::index_sequence<Is...>> { template <typename Container> static void execute (const Container& v) { foo(v[Is]...); } }; template <std::size_t N> void fooArray (const std::array<int, N>& a) { Foo

How to define a map with key as a enum and value as an integer in c++11/c++14?

蹲街弑〆低调 提交于 2019-12-13 02:39:06
问题 enum Symbols { BAR, BELL, PLUM, ORANGE, CHERRY, DOESNOTMATTER, }wheel1, wheel2, wheel3; map<Symbols[3], int> symb = {{{BAR, BAR, BAR}, 250}, {{BELL, BELL, BELL}, 20}, {{BELL, BELL, BAR}, 20}, {{PLUM, PLUM, BAR}, 14}, {{PLUM, PLUM, PLUM}, 14}, {{ORANGE, ORANGE, BAR}, 10}, {{ORANGE, ORANGE, ORANGE}, 10}, {{CHERRY, CHERRY, CHERRY}, 7}, {{CHERRY, CHERRY, DOESNOTMATTER}, 5}, {{CHERRY, DOESNOTMATTER, DOESNOTMATTER}, 2}}; I have defined an enumerator named Symbols. I am trying to create a map, who's

Uniform initialization in return statement and explicit conversion operator to bool

限于喜欢 提交于 2019-12-12 21:31:15
问题 I tried to force explicit conversion in return statement by means of using of uniform initialization syntax as it is in following: #include <iostream> #include <cstdlib> struct A { explicit operator bool () const { return false; } }; bool f() { return {A{}}; // error: no viable conversion from 'void' to 'bool' // equivalent to return bool{A{}}; at my mind } bool g() { return static_cast< bool >(A{}); } struct B { B(A) { ; } }; B h() { return {A{}}; // equivalent to return B{A{}}; } inline std

r-value causes a warning without the use of std::move

女生的网名这么多〃 提交于 2019-12-12 21:21:48
问题 Can someone help me to understand why the following code causes a warning struct A { A() : _a( 0 ) {} const int& _a; }; int main() { A a; } with warning warning: binding reference member '_a' to a temporary value [-Wdangling-field] A() : _a( 0 ) {} but this code, where std::move is used to initialize the member _a , does not: struct A { A() : _a( std::move(0) ) {} const int& _a; }; int main() { A a; } Aren't 0 and std::move( 0 ) both r-values? 回答1: This is an expression: 0 It's a very small

parsing identifiers except keywords

a 夏天 提交于 2019-12-12 20:15:19
问题 I am struggeling writing a identifier parser, which parses a alphanum string which is not a keyword. the keywords are all in a table: struct keywords_t : x3::symbols<x3::unused_type> { keywords_t() { add("for", x3::unused) ("in", x3::unused) ("while", x3::unused); } } const keywords; and the parser for a identifier should be this: auto const identifier_def = x3::lexeme[ (x3::alpha | '_') >> *(x3::alnum | '_') ]; now i try to combine these so an identifier parser fails on parsing a keyword. I

runtime-check whether an instance (Base*) override a parent function (Base::f())

烂漫一生 提交于 2019-12-12 20:06:53
问题 How to determine whether a pointer of base ( B ) class is (polymorphism-ly) override a certain virtual function of the base class? class B{ public: int aField=0; virtual void f(){}; }; class C : public B{ public: virtual void f(){aField=5;}; }; class D: public B{}; int main() { B b; C c; D d; std::vector<B*> bs; bs.push_back(&b); bs.push_back(&c); bs.push_back(&d); for(int n=0;n<3;n++){ //std::cout<<(bs[n]->f)==B::f<<std::endl; //should print "true false true" } } I tried to compare the

Are there any reasons why c++ template packs are passed using std::tuple

时光毁灭记忆、已成空白 提交于 2019-12-12 18:51:39
问题 Let's say we want to create a helper class to reverse template pack e.g. as follows: #include <tuple> #include <utility> #include <typeinfo> #include <iostream> template <class> struct sizer; template <template<class...> class Pack, class... Args> struct sizer<Pack<Args...>> { static constexpr size_t value = sizeof...(Args); }; template <class Pack, class Indices = std::make_index_sequence<sizer<Pack>::value>> struct reverse_pack; template <class... Args, size_t... I> struct reverse_pack<std: