Reputation: 87
I have following C++ code snippet :
inline std::vector<std::unique_ptr<xir::Tensor>> cloneTensorBuffer(
const std::vector<const xir::Tensor*>& tensors)
{
auto ret = std::vector<std::unique_ptr<xir::Tensor>>{};
auto type = +::DataType::XINT;
ret.reserve(tensors.size());
for (const auto& tensor : tensors) {
ret.push_back(std::unique_ptr<xir::Tensor>(xir::Tensor::create(
tensor->get_name(), tensor->get_shape(), xir::DataType{type, 8u})));
}
return ret;
}
I am not clear about the statement:
auto type = +::DataType::XINT;
What is meant by +
followed by ::
(scope resolution operator)?
Upvotes: 5
Views: 207
Reputation: 122133
There is no +::
. Its the unary +
operator and ::
operator.
::foo
refers to foo
in the global namespace. It might be necessary when there is another DataTye::XINT
in the current namespace.
The unary +
is sometimes used to trigger implicit conversions. You need to check what type ::DataType::XINT
is and what conversions it has available.
As I don't know what ::DataType::XINT
is, here is an example with a lambda expression:
template <typename T>
void foo();
int main() {
auto x = [](){};
foo(x);
foo(+x);
}
Error message (shortened) is:
<source>:6:8: error: no matching function for call to 'foo(main()::<lambda()>&)'
6 | foo(x);
<source>:7:8: error: no matching function for call to 'foo(void (*)())'
7 | foo(+x);
| ~~~^~~~
You can see that foo(x)
tries to call foo
with the lambda, while in foo(+x)
the lambda was implicitly converted to a function pointer (because of ClosureType::operator ret(*)(params)()
and +
being available for function pointers but not for the lambdas type).
Upvotes: 4
Reputation: 545488
The combination has no special meaning. +
is the regular prefix operator. In this particular case it’s probably redundant, or performs coercion to int
. However, the actual meaning might differ depending on how it’s overloaded for the type of ::DataType::XINT
.
And ::
is regular scope resolution operator. When used at the beginning of a sub-expression (i.e. without a left-hand operand), it causes lookup to be performed at the top scope, i.e. it ignores any shadowing redefinition of DataType
in a nested scope:
int x = 1;
void f() {
int x = 2;
std::cout << "inner = " << x << "\n"; // inner = 2
std::cout << "outer = " << ::x << "\n"; // outer = 1
}
Upvotes: 8