c++14

Restrict the scope of class instances accessible by multiple template parameter friend function

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:15:55
问题 I would like to know if what I am aiming for is possible. I have a class Class such that #include<iostream> template<class T> class Class; template<class T, class W> Class<W> f(Class<T>& C, const Class<T>& D); template<class T> class Class { protected: // this could be private T m_t; public: Class(): m_t(T()) {} Class(T t): m_t(t) {} T& getT() { return m_t; } template<class U, class W> friend Class<W> f(Class<U>& C, const Class<U>& D); }; template<class T, class W> Class<W> f(Class<T>& C,

Create alias for a list of types and passing it as a template parameter

萝らか妹 提交于 2019-12-10 13:04:17
问题 I am using variadic templates to implement the visitor pattern: template<typename... Types> class Visitor; template<typename Type> class Visitor<Type> { public: virtual void visit(Type &visitable) = 0; }; template<typename Type, typename... Types> class Visitor<Type, Types...>: public Visitor<Types...> { public: using Visitor<Types...>::visit; virtual void visit(Type &visitable) = 0; }; template<typename... Types> class VisitableInterface { public: virtual void accept(Visitor<Types...>

Why does std::shared_ptr<T> = std::unique_ptr<T[]> compile, while std::shared_ptr<T[]> = std::unique_ptr<T[]> does not?

最后都变了- 提交于 2019-12-10 12:59:02
问题 I explored this topic in Coliru with the following input command: g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out The test can be found here, but I have posted the code below. I used int in my example, as it's a basic type. #include <iostream> #include <memory> struct Foo{ Foo() : a_{0}, b_{1}, c_{-1}, combination_{0.5} {} int a_, b_, c_; double combination_; }; int main() { //int // *unManagedArray = new int[16]; std::unique_ptr<int[]> uniqueArrayOrigin = std::make_unique<int

Is not matching a template<typename…> to template<typename> a defect?

天大地大妈咪最大 提交于 2019-12-10 12:49:44
问题 While exploring this answer I discovered that a template that takes a parameter pack will not be accepted by a template that expects template that has a specific number of parameters. This seems to me that it is a defect since if a template can take any number of parameters, it should be able to map to a specific number. Is there a language lawyer that could explain why this is not allowed? Here is a simple example: template <typename...Ts> using pack = void; template <template <typename>

Can default function arguments “fill in” for expanded parameter packs?

依然范特西╮ 提交于 2019-12-10 12:48:51
问题 The following code fails to compile : #include <iostream> template<typename F, typename ...Args> static auto wrap(F func, Args&&... args) { return func(std::forward<Args>(args)...); } void f1(int, char, double) { std::cout << "do nada1\n"; } void f2(int, char='a', double=0.) { std::cout << "do nada2\n"; } int main() { wrap(f1, 1, 'a', 2.); wrap(f2, 1, 'a'); } g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp: In instantiation of 'auto wrap(F, Args&& ...) [with F = void(

Templated Variables Bug With Lambdas in Visual Studio?

天涯浪子 提交于 2019-12-10 12:48:21
问题 c++14 provides variable templates Which work fine in visual-studio-2017, but within lambdas they seem to fall apart. For example: template <typename T> const auto PI = std::acos(static_cast<T>(-1)); int main() { auto func = []() { cout << PI<float> << endl; }; func(); } On gcc 6.3 this outputs: 3.14159 On Visual Studio 2017 this outputs: 0.0 回答1: Wierd bug, but it seems to have a reliable workaround, which works for both, this case and the related case. In both cases forcefully activating

g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type

六眼飞鱼酱① 提交于 2019-12-10 12:44:33
问题 Another "who's right between g++ and clang++?" question for C++ standard gurus. Given the following code #include <utility> template <std::size_t N, typename = std::make_index_sequence<N>> struct foo; template <std::size_t N, std::size_t ... Is> struct foo<N, std::index_sequence<Is...>> { }; template <std::size_t N> void bar (foo<N> const &) { } int main() { bar(foo<42u>{}); } I see that g++ compile where clang++ gives the following error tmp_003-14,gcc,clang.cpp:32:4: error: no matching

Is the value of expression f() > g(), when f & g modify same global variable undefined or unspecified?

五迷三道 提交于 2019-12-10 12:43:31
问题 UPDATE : As marked by user ecatmur , it's a duplicate of In C99, is f()+g() undefined or merely unspecified? (although the questions asks about C99, but answer is unchanged for C++). And the answer is: unspecified (for both cases). Consider following C++14 code fragment: int i = 0; int x() { i++; return i;} int y() { i++; return i;} bool z = (x() > y()); // unspecified or undefined ? Is the value of z merely unspecified, or is this undefined behavior ? As per my understanding (please correct

How do I deduce auto before a function is called?

烈酒焚心 提交于 2019-12-10 12:32:20
问题 while experimenting with function return type deduction auto func(); int main() { func(); } auto func() { return 0; } error: use of ‘auto func()’ before deduction of ‘auto’ Is there a way to use this feature without needing to specify the definition before the call? With a large call tree, it becomes complicated to re-arrange functions so that their definition is seen before all of the places they are called. Surely an evaluation could be held off until a particular function definition was

eps parser that can fail for a “post-initialization” that could fail

只谈情不闲聊 提交于 2019-12-10 11:44:58
问题 I'm reading the Boost X3 Quick Start tutorial and noticed the line eps is a special spirit parser that consumes no input but is always successful. We use it to initialize the rule's synthesized attribute, to zero before anything else. [...] Using eps this way is good for doing pre and post initializations. Now I can't help but wonder if an eps_that_might_fail would be useful to do some sort of semantic/post analysis on a part of the parsed input, which could fail, to have some sort of