Disambiguation in size_of

Reference says:

Several situations often occur which result in ambiguities about the receiver or referent of method or associated function calls. These situations may include:

Multiple in-scope traits define methods with the same name for the same types
Auto-deref is undesirable; for example, distinguishing between methods on a smart pointer itself and the pointer’s referent
Methods which take no arguments, like default(), and return properties of a type, like size_of()

I really don't understand. Is the third point a subset of the first one? If yes, why is it separated? If not, what exactly does it mean? Why should that method have no arguments and return a property of the type? Could you provide examples for this?

The third bullet is separate to emphasize that even specifying the full path of the function name for ::std::mem::size_of doesn't fully specify the function used, because of the type argument.
You'd need to specify the type argument like so:
std::mem::size_of::<u8>()

Playground

1 Like

But size_of is just one function! I can't see any ambiguity here.

I need a full explanation for educational purposes.

Technically, it's not one function, but a family of functions - one function for each T in size_of::<T>. In particular, you can't assign it to a variable of type fn() -> _, unless you specify the type - playground.

1 Like

Why should that method have no arguments and return a property of the type?

Because it's useful to know some properties of types, and that function doesn't happen to need any arguments in order to be implemented.

So why shouldn't it be like it is?

3 Likes

The issue isn't merely about being useful. Rather, it explains the inherent constraints of the problem.

It does: size_of needs T: Sized, which size_of_val does not.

1 Like

But that's not the function argument nor returning value!!

Additionally, the sentence describing the third point in the reference has slight syntactic complexity

I'm looking for a CLEAR explanation please!

Is your confusion purely regarding this sentence?

  • Methods which take no arguments, like default(), and return properties of a type, like size_of()

If so, my advice is to just ignore the part that talks about size_of. This section of the reference is talking about the qualified paths introduced by RFC 132. As they say, it's used for methods and associated functions. But size_of is not a method or associated function of some type.[1]

Or in other words, that bullet should probably just be

  • Methods which take no arguments, like default()

The RFC used some imaginary SizeOf trait as an example case. Perhaps this is the origin of mentioning size_of in this section?


If your confusion is about the function std::mem::size_of itself, or about the method Default::default, I'm afraid I'm still struggling to understand exactly what you find confusing.


  1. You may need to disambiguate calls to size_of as well, but you do so with turbofish, not qualified paths. ↩︎

1 Like

I also think this part of the reference needs revision and clarification.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.