Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with a volatile type deduction in template functions?

I have a template function:

template <typename T>
inline void Acquire_Store(volatile T* ptr, T value) {
  // ...
}

When I try to invoke it like this:

volatile Node* node;
Acquire_Store(&node, static_cast<Node*>(nullptr));

The both g++, clang++ compilers say something like this:

deduced conflicting types for parameter 'T' ('volatile List::Node *' vs. 'List::Node *')

What is a correct way to invoke this template function?

Update.

Now I'm not sure about node's type - maybe, I should change it to Node* volatile node;?

I want the variable node to be volatile, but not the pointed object.

like image 731
abyss.7 Avatar asked Nov 01 '22 16:11

abyss.7


1 Answers

The substitution and deduction is not lexically like with a macro. volatile T* will not become volatile Node** but volatile Node* volatile*. The second volatile comes from the template. That makes T equal volatile Node* for the first parameter.

Try to free yourself from sticking the volatile always at the beginning, but put it qhere it belongs according to the actual type. A volatile pointer has the volatile after the star, not before it.

like image 60
Johannes Schaub - litb Avatar answered Nov 15 '22 03:11

Johannes Schaub - litb