I understand that the question sounds a little confusing but bear with me. As I hope that this example might at the bare minimum give you an idea of what I am asking.
If you have a better name for this question, please don't refrain from informing me about it and I shall change it straight away
So here we go!
Suppose I have a class
template <size_t size_of_allocated_memory_in_bytes> class malloc_ptr
inside of which I have a method
template <typename cast_type, bool should_use_c_cast> cast_type cast();
Here is the entire class;
template <size_t size_of_allocated_memory_in_bytes>
class malloc_ptr
{
private:
void* ptr;
public:
malloc_ptr()
: ptr(malloc(size_of_allocated_memory_in_bytes)) {}
template <typename cast_type , bool should_use_c_cast>
cast_type cast();
~malloc_ptr() noexcept
{ free(ptr); }
};
I understand that I can implement the function like this [with the help of https://stackoverflow.com/questions/27283261/template-function-in-a-templated-class]
template<size_t size_of_allocated_memory_in_bytes>
template<class cast_type, bool should_use_c_cast>
cast_type malloc_ptr<size_of_allocated_memory_in_bytes>::cast()
{
return reinterpret_cast<cast_type>(ptr);
}
However I only want the function to be implemented when should_use_c_cast == true
but I have no clue on how to do that.
I hope that you all could help with this. Moreover, any help would be greatly appreciated. And Thank You in advance!