explicit

How to check if type is explicitly/implicitly constructible?

喜夏-厌秋 提交于 2021-02-18 06:25:10
问题 How can it be checked that some type is explicitly (or vice versa implicitly) constructible from other type? Is it any SFINAE trick in this situation? I can write is_explicitly_constructible as a combination of std::is_constructible and std::is_convertible: #include <type_traits> template <typename Type, typename Argument> struct is_explicitly_constructible : std::bool_constant < std::is_constructible<Type, Argument>::value && !std::is_convertible<Argument, Type>::value > { }; But do I take

Explicit Copy constructor call syntax

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 06:28:17
问题 When I declare my copy constructor as explicit, calling it using = instead of () doesn't compile. Here's my code: class Base { public: explicit Base(){cout<<__PRETTY_FUNCTION__<<endl;} explicit Base(Base& b){cout <<__PRETTY_FUNCTION__<<endl;} }; int main() { Base a; Base b=a; } The compiler says: error: no matching function for call to ‘Base::Base(Base&)’ If I change it to Base b(a); It compiles fine. I thought C++ considers these two styles of instantiations the same. If I remove the

Explicit Copy constructor call syntax

冷暖自知 提交于 2021-02-11 06:27:25
问题 When I declare my copy constructor as explicit, calling it using = instead of () doesn't compile. Here's my code: class Base { public: explicit Base(){cout<<__PRETTY_FUNCTION__<<endl;} explicit Base(Base& b){cout <<__PRETTY_FUNCTION__<<endl;} }; int main() { Base a; Base b=a; } The compiler says: error: no matching function for call to ‘Base::Base(Base&)’ If I change it to Base b(a); It compiles fine. I thought C++ considers these two styles of instantiations the same. If I remove the

Why does position of explicit template instantiation matter

天大地大妈咪最大 提交于 2021-01-27 04:47:08
问题 Say I declare a template class A in a.h #include <iostream> template<bool b> class A { public: void print(std::ostream& out); }; And define the print method in a.cpp (with explicit instatiation for true and false ) #include "a.h" template<bool b> void A<b>::print(std::ostream& out) { out << "A" << b; } template class A<true>; template class A<false>; An example main main program in main.cpp could be #include "a.h" int main() { A<true> a; a.print(std::cout); } The small project above compiles

Why does position of explicit template instantiation matter

萝らか妹 提交于 2021-01-27 04:45:12
问题 Say I declare a template class A in a.h #include <iostream> template<bool b> class A { public: void print(std::ostream& out); }; And define the print method in a.cpp (with explicit instatiation for true and false ) #include "a.h" template<bool b> void A<b>::print(std::ostream& out) { out << "A" << b; } template class A<true>; template class A<false>; An example main main program in main.cpp could be #include "a.h" int main() { A<true> a; a.print(std::cout); } The small project above compiles

Construct tuple by passing the same argument to each element with explicit constructor

天大地大妈咪最大 提交于 2021-01-23 06:51:12
问题 The following works fine on Visual C++ 2015 Update 2. Note that A is non-copyable and A::A is explicit . #include <iostream> #include <tuple> struct A { explicit A(int i) { std::cout << i << " "; } // non-copyable A(const A&) = delete; A& operator=(const A&) = delete; }; template <class... Ts> struct B { std::tuple<Ts...> ts; B(int i) : ts((sizeof(Ts), i)...) { } }; int main() { B<A, A, A, A> b(42); } The goal is to pass the same argument to all tuple elements. It correctly outputs: 42 42 42

starting android service using explicit vs implicit intent

流过昼夜 提交于 2021-01-21 04:01:37
问题 According to the standard Android documentation, the prefered way to start a service (started service that is) is to use an explicit intent like this: // Using explicit intent: Intent serviceIntent = new Intent(getApplicationContext(), MyService.class); // or: Intent serviceIntent = new Intent(this, MyService.class); startService(serviceIntent); You can also start/stop a service using an implicit intent with an action string specified in the manifest, like this: // Using implicit intent:

usage of explicit keyword for constructors

拥有回忆 提交于 2020-08-06 07:42:13
问题 I was trying to understand the usage of explicit keyword in c++ and looked at this question on SO What does the explicit keyword mean in C++? However, examples listed there (actually both top two answers) are not very clear regarding the usage. For example, // classes example #include <iostream> using namespace std; class String { public: explicit String(int n); // allocate n bytes to the String object String(const char *p); // initializes object with char *p }; String::String(int n) { cout<<

Best (safest) way to convert from double to int

谁说胖子不能爱 提交于 2020-07-03 01:51:14
问题 I'm curious as to the best way to convert a double to an int. Runtime safety is my primary concern here (it doesn't necessarily have to be the fastest method, but that would be my secondary concern). I've left a few options I can come up with below. Can anyone weigh in on which is best practice? Any better ways to accomplish this that I haven't listed? double foo = 1; int bar; // Option 1 bool parsed = Int32.TryParse(foo.ToString(), out bar); if (parsed) { //... } // Option 2 bar = Convert