Questions tagged [explicit]

In C++ specifies constructors and conversion operators that don't allow implicit conversions or copy-initialization. In C# declares a user-defined type conversion operator that must be invoked with a cast. For MS SQL Server for-xml-EXPLICIT mode use the [for-xml-explicit] tag

405 questions
3
votes
3 answers

Why does explicit constructor of boost::shared_array cause an error?

boost::shared_array x(new char const *[n]); In the line above (n is integer number not greater than 100) I'm creating char const**(const char**) and putting it to smart pointer x for arrays to be deleted when x is deleted. And for me…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
3
votes
1 answer

Copy constructor and overloaded addition operator

I am reviewing operator overloading in C++. Just for fun I am implementing a BigInt class. The first operator I want to overload for it is the addition operator. I have decided to overload this operator as a friend non-member function. Here's a MWE…
3
votes
2 answers

Can't Define an initializer_list in cbegin

So I can do this: for(const auto i : { 13, 42 }) cout << i << ' '; But I can't do this: copy_n(cbegin({ 13, 42 }), 2, ostream_iterator(cout, " ")); It gives me the error: error: no matching function for call to cbegin(
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
4 answers

NULL In a Class Destructor

Possible Duplicate: Is it worth setting pointers to NULL in a destructor? Is it pointless to set a pointer (which allocates heap memory) to NULL in the destructor? class SampleClass { public: SampleClass( int Init = 0 ) { …
DeadCapacitor
  • 209
  • 4
  • 11
3
votes
1 answer

Conditional Default Constructor compiles in GCC but not MSVC

I'm working on creating a template class where I have the following constraints: If the type of the class is generic, it should have normal constructors If the type of the class is NOT generic, it should have explicit constructors. The solution I…
Xirema
  • 19,889
  • 4
  • 32
  • 68
3
votes
3 answers

Using explicit constructor

class foo { public: explicit foo(int) { std::cout<<"int constructor"; } }; int main() { foo f(0.1); return 0; } I thought explicit keyword was being used to prevent unwanted type conversions, but the above code still works and outputs…
J. Doe
  • 33
  • 1
  • 4
3
votes
1 answer

The explicit keyword and constructors with more than one parameter for C++11

In general, I've heard it's good practice to use the explicit keyword on constructors with a single argument. However, as of C++11, constructors with more than one argument can be used for implicit conversions. As such, is it good practice to apply…
Josh
  • 700
  • 5
  • 14
3
votes
1 answer

Is it legit code to tag an object like this?

I've once written a piece of code to add a name to a Task. The code below seems to do the same, but with less code. But I wonder, is it legit. Is it production code ready. What about garbage collection? What about the instance of the class being…
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
3
votes
2 answers

shared_ptr assignment notation implicit conversion

considering following code , Why I can’t use the assignment notation here , Why that is considered to be an implicit conversion. shared_ptr pNico = new string("nico"); // ERROR implicit conversion shared_ptr pNico{new…
Adib
  • 723
  • 5
  • 22
3
votes
3 answers

Declaring an instance of an explicit specializtion of a template within a regular class

I can't get this to compile at all. I may not be possible but I don't know why it should not be. class A { template class B { int test() { return 0; } }; //template <> class B; <-with this, namepace error B
user438938
  • 71
  • 4
3
votes
1 answer

Why would the param_type constructor be explicit for a random distribution?

I'm trying to compile this program (see it live here): int main() { std::random_device engine; std::uniform_int_distribution dis; std::cout << dis(engine, {0, 5}) << std::endl; } But it fails with the error message: error: converting…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
3
votes
2 answers

Javascript use explicit self/window objects to improve performance

I read on MSDN that to improve scripting efficiency, you can use self to make implicit window references explicit. Do you know if this is true? Does this basically mean that for instance calling self.location is somewhay more efficient than calling…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
3
votes
1 answer

Explicit module exports in Frege

I am posting this after reading the Frege language specification and looking for examples using search engines. I hope I have not overlooked an obvious answer. I am trying to port some Haskell code to Frege and I cannot find any documentation…
Giorgio
  • 5,023
  • 6
  • 41
  • 71
3
votes
1 answer

Explicit call to the destructor

Can i still access an object after making an explicit call to its destructor? for example, class A{ public: A(){ cout<<"Constructor\n"; x=5; } ~A(){ …
grv95
  • 33
  • 2
3
votes
3 answers

I want to extend std::string, but not for the reason you might think

I have a method that effectively takes a string. However, there is a very limited subset of strings I want to use. I was thinking of typedef'ing std::string as some class, and call the functions explicit. I'm not sure that would work, however.…
alexgolec
  • 26,898
  • 33
  • 107
  • 159