explicit keyword [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does the explicit keyword in C++ mean?
what does the keyword explicit mean?

C++ constructors that have just one parameter automatically perform implicit type conversion. For example, if you pass an int when the constructor expects a string pointer parameter, the compiler will add the code it must have to convert the int to a string pointer. However, you might not always want this automatic behavior.
You can add explicit to the constructor declaration to prevent implicit conversions. This forces the code to either use a parameter of the correct type, or cast the parameter to the correct type. That is, if the cast is not visibly expressed in code, an error will result.
explicit (C++)

Related

what's the meaning of no parameter name but default value [duplicate]

This question already has answers here:
default parameters without name in c ++
(3 answers)
Closed 9 months ago.
i'm learning stl, there is member function in new_allocator class:
pointer
allocate(size_type __n, const void* = 0)
{
if (__n > this->max_size())
std::__throw_bad_alloc();
return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
}
there is no name but default value in second paramter,what's is the meaning like doing this?
The parameter name isn’t required if the function doesn’t need to reference the parameter, and leaving it unnamed avoids a compiler warning about an unused parameter.
The default value allows callers to call the function with a single argument, and the compiler will treat it as if they called it with two arguments, with the second argument being specified as the default value.
As for why the programmer might include a second, unnamed, unused argument rather than just having the function take a single argument — one reason might be that the user wants the function to fit a particular interface that takes two arguments (eg for inheritance or SFINAE purposes)… or maybe they are planning to change the function to actually use the second argument later, but haven’t got around to doing it yet.

is 'auto' in c++ similar to 'Object' in java? [duplicate]

This question already has answers here:
C++ auto keyword. Why is it magic?
(8 answers)
Closed 12 months ago.
In Java, Object takes the place of various class objects, but casting is needed. In the piece of code below, auto seems to be doing the same only that it does not need to be casted with a type variable.
It seems like a lazy (efficient?) way to progress through code. I might get accustomed to using auto instead of a specific type (in the example below, it would be 'int index'). There might be an occasion(s) where such incorporation would not be recommended.
piece of code:
vector<int> fCount(1001,0);
for(auto index : list)
{
++fCount[index];
}
Actually NO. Object in Java differs from auto in C++. In C++, auto keyword only can be applied to any type that can be deduced in compile-time, not run-time. In the code snippet you given, although the index is deduced to be an int, but if you try to use to use index in wherever context that the int cannot be used, compiler will complain.
auto is just a syntactic sugar that modern C++ offers, that you do not have to specify the type when compiler can deduce from the expression. Although you do not have to specify the type explicitly, compiler specifies it in implicit way. After the compiler deduced the type, you cannot use the variable as other type(if it cannot be implictly converted)

Why does using parentheses with a default constructor result in creation of the variable? [duplicate]

This question already has answers here:
What is the purpose of a declaration like int (x); or int (x) = 10;
(2 answers)
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
After watching Louis Brandy talk at CppCon 2017 I was shocked to discover that this code actually compiles:
#include <string>
int main() {
std::string(foo);
return 0;
}
And for some reason std::string(foo) it is identical to std::string foo i.e. declaring a variable. I find it absolutely counterintuitive and can't see any reason for C++ to work that way. I would expect this to give an error about undefined identifier foo.
It actually makes expressions like token1(token2) have even more possible interpretations than I previously thought.
So my question is: what is the reason for this horror? When is this rule actually necessary?
P.S. Sorry for the poorly worded title, please, feel free to change it!
Since this question is tagged language-lawyer, the direct answer is that, from [stmt.ambig]:
There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those cases the statement is a declaration.
And, similarly, for functions, in [dcl.ambig.res]:
The ambiguity arising from the similarity between a function-style cast and a declaration mentioned in [stmt.ambig] can also occur in the context of a declaration. In that context, the choice is between a function declaration with a redundant set of parentheses around a parameter name and an object declaration with a function-style cast as the initializer. Just as for the ambiguities mentioned in [stmt.ambig], the resolution is to consider any construct that could possibly be a declaration a declaration.
Hence:
Why oh why is std::string("foo") so different from std::string(foo)
The former cannot be a declaration. The latter can be a declaration, with a redundant set of parentheses. Thus, the former isn't a declaration and the latter is.
The underlying issue is that, grammaticaly, declarators can start with a ( which could make it indistinguishable from a function-style explicit type conversion. Rather than come up with arbitrary complex rules to try to determine what the user meant, the language just picks one, and it's easy enough for the user to fix the code to actually do what he meant.

Assessment of Template Parameter [duplicate]

This question already has answers here:
Identifying primitive types in templates
(8 answers)
Closed 6 years ago.
Is there a ready function which can take a template parameter as an argument and determine that it is a user defined types(class or struct) or built-in data types(int, float, char...) ?
You're looking for the std::is_arithmetic template, which determines if the template parameter is an integer or a floating point type.
By process of elimination, the only remaining options are: pointer or a reference, a class, and void. Maybe an enum of some kind, too. It's unclear from your question how you want to classify those, but, if necessary, adding some additional checks on top of std::is_arithmetic should make it possible to disambiguate the given type further.

Is it not type safe if you are allowed to assign an int to a float (or vice-versa)? [duplicate]

This question already has answers here:
Why does C++ allow implicit conversion from int to unsigned int?
(5 answers)
Closed 6 years ago.
Based on what I know about type safety, a type safe language does not allow you to assign a variable of one type to a variable of a different type (unless you perform an explicit conversion). But in C and C++ I can do the following:
int i = 12345;
float f = i; // this is allowed
Is this operation considered not type safe?
[..] a type safe language does not allow you to assign a variable of one type to a variable of a different type (unless you perform an explicit conversion).
I don't think that this would be a good definition. Rather I'd say that a type safe language does not allow operations specific to / designed for a type X to be performed on a value of a other type Y if those two types are not considered compatible under some metric. That's what's known as strong typing (not to be confused with static typing).
Is this operation considered not type safe?
It's a well defined (albeit implicit) conversion from one type to another.
What seems to be bugging you is the part that this conversion is implicit and not explicit. I don't know of a distinct "term" / word to describe that a language allows implicit conversions, though.
That said, C++ surely is not type safe in all respects.