explicit copy constructor


, I noticed the following in the "C++ Coding Standards" book. Why should the code on line 30 be in error Transmogrify accepts a (B obj), and we are passing it a B obj. I understand why the other call ...
Posted On: Monday 26th of November 2012 12:46:29 AM Total Views:  353
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




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

172

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

151

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

163

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

178

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

159

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

201

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

155

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

193

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

189

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

338

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

112

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

140

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

132

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

127

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

131

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

158

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

152

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!

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

Hi all ! What is the 'explicit' mean as a key word Thank you!...
VIEWS ON THIS POST

98

Posted on:

Monday 26th November 2012
View Replies!