Skip to content

Calling std::mem::size_of::<T> in a generic constexpr context errors. #45598

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mystor opened this issue Oct 28, 2017 · 2 comments
Closed

Calling std::mem::size_of::<T> in a generic constexpr context errors. #45598

mystor opened this issue Oct 28, 2017 · 2 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) I-compiletime Issue: Problems and improvements with respect to compile times.

Comments

@mystor
Copy link
Contributor

mystor commented Oct 28, 2017

This code fails to build right now, while it should succeed (as std::mem::size_of::<T> is const):

use std::mem;
use std::marker::PhantomData;
struct U<T>([u8; mem::size_of::<T>()], PhantomData<T>);
fn main() {}
@mystor
Copy link
Contributor Author

mystor commented Oct 28, 2017

   Compiling playground v0.0.1 (file:///playground)
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
 --> src/main.rs:3:18
  |
3 | struct U<T>([u8; mem::size_of::<T>()], PhantomData<T>);
  |                  ^^^^^^^^^^^^^^^^^ `T` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `T`
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: required by `std::mem::size_of`

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

@frewsxcv frewsxcv added A-const-fn I-compiletime Issue: Problems and improvements with respect to compile times. labels Oct 28, 2017
@frewsxcv
Copy link
Member

Duplicate of #43408?

@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) I-compiletime Issue: Problems and improvements with respect to compile times.
Projects
None yet
Development

No branches or pull requests

3 participants