class-template

Will consteval functions allow template parameters dependent on function arguments?

五迷三道 提交于 2020-03-10 10:48:31
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int

Is static member variable initialized in a template class if the static menber is not used?

安稳与你 提交于 2020-01-03 21:02:10
问题 Is static member variable initialized in a template class if the static member is not used? I use it to register the type. template<class T> class A { static bool d; }; template<class T> bool A<T>::d = [](){regist<A<T>>(); return true;}(); int main() { A<int> a; return 0; } I find a way to test it. It prints 1 other than 2. The regist() is not called abd the static member is not initialized. My testing is on VC110 compilter. And I also test it online #include <iostream> using namespace std;

When are member functions of a templated class instantiated?

橙三吉。 提交于 2020-01-01 17:56:07
问题 Consider the following example: template<typename T> class Base { public: inline void fooBase () { T t; // The following error only occurs when class ABC is not defined at the end of the file: "error: t uses undefined class ABC" } protected: }; class ABC; class DEF; class Derived : public Base<ABC> { public: void fooDerived () { DEF def; // error: def uses undefined class DEF } }; Derived derived; void foo () { derived.fooBase (); } class ABC {}; class DEF {}; Question(s) Why is the compiler

class template without public constructor as member of another class template

大城市里の小女人 提交于 2019-12-25 03:52:05
问题 I have a class template Shape , which contains information about certain shapes (which can be three- or two-dimensional). I only want a few predefined shapes (cube, sphere and square) to be available. All these predefined shapes have the same properties (so the cube always has the same volume, and I only need to remember the properties of one cube). To inhibit someone from creating other Shape s, I made the constructor private : // Flag for the possible shapes enum class Tag { SPHERE, CUBE,

Why is this call to swap() ambiguous?

扶醉桌前 提交于 2019-12-21 03:15:26
问题 The following program #include <algorithm> #include <utility> #include <memory> namespace my_namespace { template<class T> void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } template<class T, class Alloc = std::allocator<T>> class foo {}; } int main() { my_namespace::foo<int> *a, *b; using my_namespace::swap; swap(a,b); return 0; } causes both g++ and clang to issue the following compiler error on my system: $ clang -std=c++11 swap_repro.cpp -I. swap_repro

Class template in PHP like in C++

强颜欢笑 提交于 2019-12-11 09:58:02
问题 Is is possible to create class template in PHP as in C++ ? PHP probably does not have a similar language structure (like template key words in C++ ), but maybe there is some clever trick to achieve similar functionality? I have a Point class that I would like to convert to a template. In class I use typing arguments, therefore, for each class, I would like to pass to the Point method I have to create a new copy of Point class with appropriate types of arguments. This is sample form C++ :

Define friend function template of class template

好久不见. 提交于 2019-12-10 16:32:00
问题 I want to define a function template of a class template. The code looks like this. template<int M> struct test{ private: int value; template<int N = 2 * M> friend auto foo(test const t){ test<N> r; r.value = t.value; return r; } }; int main(){ test<1> t; foo(t);// expected to return test<2> foo<1>(t);// expected to return test<1> foo<3>(t);// expected to return test<3> } But it won't compile. Compared with previous problems, the following lists the differences. The result of the function

C++20 designated initializers with templated types

岁酱吖の 提交于 2019-12-08 17:00:48
问题 How are designated initializers (C++20) supposed to work with CTAD? This code works fine in gcc9.2, but fails with clang8 template <typename int_t=int, typename float_t=float> struct my_pair { int_t first; float_t second; }; template<typename ... ts> my_pair(ts...) -> my_pair<ts...>; int main() { my_pair x{.first = 20, .second = 20.f}; static_assert( std::is_same_v<decltype(x.first), int> ); static_assert( std::is_same_v<decltype(x.second), float> ); } Is this supposed to be valid? See an

g++ c++17 class template argument deduction not working in a very specific case

你。 提交于 2019-12-08 15:53:20
问题 I have the following code: template <class T> class lit { public: lit(T l) : val(l) {} T val; }; template <class T> class cat { public: cat(lit<T> const& a, lit<T> const& b) : a(a), b(b) {} lit<T> const& a; lit<T> const& b; }; template <class T> cat<T> operator+(lit<T> const& a, lit<T> const& b) { return cat(a, b); } int main() { auto r1 = cat((lit ('b')), lit('d')); // compiles auto r2 = (lit ('b')) + lit('d') ; // doesn't compile auto r3 = lit ('b') + lit('d') ; // compiles auto r4 = (lit (

C++ - Defining class template (header/source file)

大城市里の小女人 提交于 2019-12-06 13:29:46
问题 I want to create a processor in voreen (like this one .cpp | .h) porting this OTB-Application: http://hg.orfeo-toolbox.org/OTB/file/ca4366bb972e/Applications/Segmentation/otbSegmentation.cxx I have coded almost all the parameters into properties, etc but.. If you look at like 376, you'll see a class template of FloatVectorImageType::SizeType, a typedef type. Im not familiar with c++ templates so my first question was where should I put this template's implementation, in the .cpp or .h file of