1 year ago

#363641

test-img

roulette01

pointer type dependent on if else conditional

Currently, I have code that looks like below:

template <class T> 
void func(const Tensor& input) {
  T* ptr = input.get_ptr();

  // we do some stuff with ptr below
}

I want to change the code to the following

template <class T> 
void func(const Tensor& input) {
  unknown_type* ptr;
  if (input.is_wrapped_int_tensor) {
    ptr = reintrepret_cast<type_depends_on_type_of_input_get_ptr*>(input.get_ptr());
  } else {
    ptr = input.get_ptr();
  }

  // we do some stuff with ptr below
}

Essentially, our Tensors can be floats or integers, but they're not normal C++ integers, they're custom integers, e.g., we have a custom_int8 struct, and the reinterpret_cast would convert the custom_int8* to a regular int8_t*. We have code that produces type_depends_on_type_of_input_get_ptr.

I'm having trouble figuring out what to use for unknown_type. In the first snippet it was simply T, but here, because we have to reinterpret_cast if we have an int tensor, I can no longer use T. What can I use here that would be able to handle both cases?

I think one solution can be

auto* ptr = (input.is_wrapped_int_tensor ? 
    reintrepret_cast<type_depends_on_type_of_input_get_ptr*>(input.get_ptr())
    : input.get_ptr();

Is it possible to do this with regular if/else statements?

c++

pointers

reinterpret-cast

0 Answers

Your Answer

Accepted video resources