What is the 'explicit' mean as a key word?


Hi all ! What is the 'explicit' mean as a key word Thank you!...
Posted On: Monday 26th of November 2012 12:06:16 AM Total Views:  99
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




ADL lookup with explicit template parameters

Hi to all, Recently a bug was reported against a Boost library regarding an ADL customization point. VC 7.1 and VC 8.0 compile fine the library but gcc reports an error. This ADL issue has been discussed but it's not clear for me which compiler is right. The test case is simple. A container takes the pointer type as a template argument and wants to treat this pointer generically. The container wants to make an static cast in the pointer and calls "static_pointer_cast(source_ptr);". static_pointer_cast has been adopted as the casting function for std::tr1::shared_ptr, so it's logical to be overloaded by other smart pointers, like with get_pointer() is going to be used to obtain a raw pointer generically. It happens that VC 7.1 and VC 8.0 compile this fine but gcc compiles only if the static_pointer_cast code for the smart pointer is declared before the calling point. This causes problems to library users, because compilation success depends on header include order. This is a simplified test case: //////////////////////////////////////// //raw_ptr.hpp header: //////////////////////////////////////// namespace raw_ptr { //static_pointer_cast overload for raw pointers template inline T* static_pointer_cast(U *ptr) { return static_cast(ptr); } } //namespace raw_ptr //////////////////////////////////////// //container.hpp header: //////////////////////////////////////// namespace dummy { template class container { public: void func() { using raw_ptr::static_pointer_cast; static_pointer_cast(Pointer()); } }; } //namespace dummy { //////////////////////////////////////// //smart_ptr.hpp header: //////////////////////////////////////// namespace dummy { template class smart_ptr {}; template smart_ptr static_pointer_cast(const smart_ptr &u) { return smart_ptr(); } } //namespace dummy { int main() { using namespace dummy; typedef container my_container; my_container cont; cont.func(); typedef container my_container2; my_container2 cont2; cont2.func(); return 0; } This fails in gcc (3.4 and 4.1) with: "no matching function for call to 'static_pointer_cast(dummy::smart_ptr)" error. The questions are: -> Is gcc right or it should should kick ADL lookup like VC If gcc is right, generic casts expression would be unsuitable as customization points. On the other hand, if the cast operation is declared without explicit template parameters: //Declaration template smart_ptr static_pointer_cast(const smart_ptr &u, T * = 0) { return smart_ptr(); } //Call static_pointer_cast(Pointer(), (int*)0); both compilers compile this fine. Should the "function(params...)" expression disallow ADL To ADL or not to ADL, that's the question.
VIEWS ON THIS POST

161

Posted on:

Monday 5th November 2012
View Replies!

explicit instantiation of mpl typelists

I'd like to meta-program explicit instantiations of my classes based on the contents of mpl typelists (such as boost::mpl::vector instances), so that i can control what to instantiate from one header file. So far, I wrote the following: // BEGIN CODE #include #include template struct instantiator { template struct apply { typedef D type; // template D; // CAN EXPLICIT INSTANTIATION BE METAPROGRAMMED \t }; }; template struct typelist_instantiator : boost::mpl::transform_view {}; struct empty_base {}; struct make_pair { template struct apply { typedef std:air type; }; }; template struct tuple0: boost::mpl::fold::type {}; // instantiate default-constructor of D for each V in TypeList template void instantiate_typelist() { tuple0 x; } // definition of template DataArray omitted; // definition of typelists::value_types omitted; // explicit instantiation of instantiate_typelist template void instantiate_typelist(); // END CODE As you can see, explicit instantiation of the instantiate_typelist function almost does the trick, except that it requires that the instantiated class template has a default constructor, and I rely on a undocumented compiler feature that instantiating a constructor of a template class gives a result similar to explicit template instantiation. With explicit instantiation I would have a better guarantee that static member functions (such as Create functions) and static data members (such as MetaClass objects that register such Create functions) are instantiated too. Furthermore, I'd like to dispatch to the instance of an operator class template that relates to the dynamic type of an object (such as done with the double dispatch pattern that requires the visitor to have a virtual function for each of the possible types of the inviter). So, to summarize my question to you: How can one put a mpl typelists to work Maarten. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

177

Posted on:

Monday 5th November 2012
View Replies!

explicit and not explicit constructors

Hi. I've .... class functor { .... functor(): implementation_(0) { } explicit functor ( int ): implementation_(0) { } explicit functor ( char * ): implementation_(0) { } template functor(functionType function): implementation_(new implementation::handler(function)) { } ..... } and I've following code: void someFunction( int a = 0 ); void someFunction( functor a = 0 ); calling of someFunction without parameters makes error "ambiguous call to overloaded function" because template constructor cover explicit constructors. The question is: "How to avoid of covering explicit constructors and leave template constructor". -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

152

Posted on:

Monday 5th November 2012
View Replies!

explicit contructors

uvts_cvs@yahoo.com wrote: > everybody. > > Consider this tiny class. > > struct foo > { > explicit foo(int){} > }; > > Declaring this constructor explicit forbids something like: > > foo bar(0); // owed > bar = 1; // Forbidden line! > > But it also forbids: > foo bar = 0; > > Can anybody explain me why we need to forbid this line when "foo > bar(0);" is allowed. Aren't they equivalent 'cause they both mean > "construct a foo object starting from 1 just calling the specific > constructor foo::foo(int)" > foo bar = 0; means construct a temporary object of type foo initialized with the value 0, and use the copy constructor to initialize bar with that temporary object. Since the construction of the temporary object is not allowed, the statement isn't valid. --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
VIEWS ON THIS POST

164

Posted on:

Monday 5th November 2012
View Replies!

explicit enum

Anyone know if they're considering adding "explicit enum" to the language I've read about such a proposal a few times. The essence of it is that when you write: explicit enum Colour { red, green, blue }; "red", "green" and "blue" aren't in the same scope as "Colour"; you've to qualify them: int main() { int k = red; /* Error */ int k = Colour::red; } I think it would be a brilliant addition. -Toms --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ] , In article , Greg Herlihy writes >There are several other significant improvements put forth in the >proposal which can be found at this address: > >http://www.open-std.org/jtc1/sc22/wg...2004/n1579.pdf That is an old paper and much more is under consideration because WG21 tries to avoid taking proposals in isolation. The proposals wrt strong typedefs (see papers by Walter Brown from Fermilab) have a considerable overlap with Miller's proposals for strong enums. One thing we are considering is whether we need both and if so, how much of the strong enum proposals should be retained. -- Francis Glassborow ACCU Author of 'You Can Do It!' see http://www.spellen.org/youcandoit For project ideas and contributions: http://www.spellen.org/youcandoit/projects --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
VIEWS ON THIS POST

182

Posted on:

Monday 5th November 2012
View Replies!

explicit keyword

, I am reading the C++ Programming Language book, but can not find the function of explicit keyword of constructor. Could anyone explain its usage or refer some learning materials please
VIEWS ON THIS POST

161

Posted on:

Monday 5th November 2012
View Replies!

copy ctor and 'explicit' qualifier

I have the following understanding: Please let me know if it is correct: copy ctor of a class is NOT declared with 'explicit' qualifier because if it is declared 'explicit', then the class cannot be used with standard library container. Is this reasoning correct Or is there some other reason for NOT declaring the copy ctor with 'explicit' qualifier Kindly clarify.
VIEWS ON THIS POST

207

Posted on:

Wednesday 7th November 2012
View Replies!

what is the advantage of explicit over imolicit in c++

I have a doubt regarding explicit and implicit declaration in c++. what is the advantage of explicit over implicit in c++. is it always better to use explicit
VIEWS ON THIS POST

158

Posted on:

Wednesday 7th November 2012
View Replies!

explicit conversion operators

I was looking at the new explicit conversion operators in the standard. The writing at the start of 12.3.2/2 doesn't make sense. "A conversion function may be explicit (7.1.2), in which case it is only considered as a user-defined conversion for direct-initialization (8.5)." That sentence is ok. "Otherwise, user-defined conversions are not restricted to use in assignments and initializations." This one contradicts the other. Initialisation using the = symbol is not assignment. Is the g() function meant to be an example for 12.3.2/1 Fraser.
VIEWS ON THIS POST

196

Posted on:

Wednesday 7th November 2012
View Replies!

Declaring explicit specialization of member function.

Again I find myself bogged down in template syntax. Here I have a class with a template member function. I know ahead of time what template parameters I will be passing to the function. I do not want to define the function in a header, I want to define it in a different translation unit. Here is some code: === a.h === class A { private: template void f_(T *); // the template... public: // ...will only be used with T = char and T = float. void f (char *p) { f_(p); } void f (float *p) { f_(p); } }; === a.cpp === #include "a.h" #include // i want T=char and T=float code to be generated here. template void A::f_ (T *) { std::cout
VIEWS ON THIS POST

194

Posted on:

Wednesday 7th November 2012
View Replies!

Is finding definitions of explicitly specialized template functionscompiler-defined?

Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter), inlined constructor implementation, and also declares a template function f. The file main.cpp contains some test code and also the default implementation for f(). The file spec.cpp contains some explicitly specialized stuff: ==== A.h ==== #include using std::cout; using std::endl; template struct A { A () { cout
VIEWS ON THIS POST

351

Posted on:

Wednesday 7th November 2012
View Replies!

Dummies question about "explicit"

Hi I though I knew what the explicit keyword did , but can someone explain to me why the following code doesn't compile class foo { public: explicit foo(int i) { } }; main() { foo f(1); foo g = 2; On 2008-01-30 06:53:51 -0500, Boltar said: > foo f(1); > foo g = 2; > I was always under the impression that the 2 types of initialisation > in main() were equivalent. Obviously not. Whats the difference The difference is that the first one constructs an object of type foo, initialized with the value 1. The second creates a temporary object of type foo, initialized with the value 2, then copies the temporary to g. The compiler is permitted to skip the copy and initialize g directly, but only if the full form would be legal. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)
VIEWS ON THIS POST

113

Posted on:

Wednesday 7th November 2012
View Replies!

push_back and explicit constructors

If I read n2461 correctly then the following code: struct Foo { explicit Foo(int) { } }; int main() { std::vector fv; fv.push_back(1); } is valid and would insert a Foo(1) instance in fv. This would be a change from 14882:2003 where it was a compile error. Is this an intended change or not --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
VIEWS ON THIS POST

140

Posted on:

Wednesday 7th November 2012
View Replies!

explicit template args

I'm trying to override boost::lexical_cast for a particular type. I think I'm doing it right but it isn't compiling. Says "illegal use of explicit template arguments". Code: namespace boost { template esi::utility::strange_double lexical_cast(Source in) { return lexical_cast(in); } } strange_double has an implicit constructor so the return should be ok. A similar function also doesn't compile: template < typename unit_t, typename type_t, typename Source > esi::units::quantity lexical_cast(Source in) { return lexical_cast(in) * unit_t(); } Use of cast is something like: quantity q = boost::lexical_cast(argv[0]); What am I doing wrong Help me understand the flaw in my understanding of templates that is causing this error. The website for the compiler error isn't much help, it doesn't seem to speak to what my code is actually attempting...it seems to me that it should work still. http://msdn2.microsoft.com/en-us/lib...x6(VS.80).aspx
VIEWS ON THIS POST

142

Posted on:

Wednesday 7th November 2012
View Replies!

explicit ctor in vector

In the standard library vector, the ctor explicit vector::vector(size_type __n) is declared as explicit. I am unable to get the reason for it being declared as explicit. Kindly clarify with an example. Does the standard require that this ctor should ...
VIEWS ON THIS POST

134

Posted on:

Saturday 10th November 2012
View Replies!

explicit copy constructor

folks, I am trying to make my copy constructor explicit, but when the scenario below comes into being, weird things happen. Here is the code snippet: #include using namespace std; class Base { public: Base() { } Base(const ...
VIEWS ON THIS POST

128

Posted on:

Saturday 10th November 2012
View Replies!

Integrating an explicit function of x from a to b

Is this an efficient way to integrate an explicit function of x from a to b: #include using namespace std; class Integrate { public: Integrate(long double (*f)(long double& x)):fn(f){} long double operator()(long double& a, long double& b)const; private: long ...
VIEWS ON THIS POST

133

Posted on:

Saturday 10th November 2012
View Replies!

shared_ptr and real world (explicitly loaded DLLs)

Classes with virtual functions and inline constructors can not be used across different modules (DLLs) if these modules are unloaded during execution of the program. The reason is that vtable created in inline constructor can have pointers into address space ...
VIEWS ON THIS POST

162

Posted on:

Sunday 11th November 2012
View Replies!

How to do explicit specialization of non-type template function

I have a quetion how to specialization a non-type templat function, I use VC++ 2005. My code is: #include "stdafx.h" enum VariableType { enumASCII = 1, enumDOUBLE = 2, enumDURATION = 3, enumINTEGER = 4 }; template< VariableType > ...
VIEWS ON THIS POST

157

Posted on:

Sunday 25th November 2012
View Replies!

explicit call of constructor and destructor

Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so class Trial { public: Trial() ...
VIEWS ON THIS POST

118

Posted on:

Sunday 25th November 2012
View Replies!