• Advertisement
Sign in to follow this  

detect alias statically?

This topic is 3036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have overloaded operator= to accept an expression template. I want operator= to behave differently if the expression template aliases the 'this' object...if 'this' is not aliased as any of the operands in the expression template, then one less temporary is needed to evaluate the expression. I have been unable to think of an efficient way to implement this. It is known at compile time whether or not there is an alias, yet I cannot use the 'this' pointer as a template argument and that restricts me from using it to enable a separate function. I could check the value of 'this' pointer manually, but finding an alias this way would take O(n) run-time branches if there were n literals in the expression! I wonder if there is a more clever way?

Share this post


Link to post
Share on other sites
Advertisement
Only addresses of variables with external linkage can be given to a template argument list:

template <int *P> struct foo {};
int x; // < okay, implicitly has external linkage
foo<&x> foox;


template <int *P> struct foo {};
extern int x; // < okay, external linkage
foo<&x> foox;


namespace { // anonymous namespace
template <int *P> struct foo {};
int x; // < okay, x is only visible in this translation unit,
// < but has external linkage
foo<&x> foox;
}


template <int *P> struct foo {};
static int x; // < error, x has not external linkage
foo<&x> foox;



Further, you can't pass pointer variables or consts to templates, as they are not immutable (whereas the address of a variable or external linkage is a compile time constant):

template <int *P> struct foo {};
const int *x;
foo<x> foox; // nope, not possible



this is not a constant, but an implicitly declared function argument, i.e. a mutable pointer variable. Also, for a pointer variable it can not be decided whether the pointee has external linkage or not. So a double no-go.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement