Value of a const static field in function return at the compile-time

P

psujkov

Hi everybody,

let us have some class A with const static int variable var with
compile-time well-known value. let us have some function f(), which
has return type of A. and let us have some template struct with non-
type template parameter : template <int a> Z. having it all there is a
question : why we cannot write smth like this : Z< f()::var> z ? there
is no need in real call to function f - it can be not defined as all
(declared only), because all we need is a static const variable value.
So why compiler does not let us to do so ? N.B. the one and only
operator that works in this example is sizeof, but I cannot see ant
real difference between sizeof and getting static const variable
value. And is there some way to avoid this error ? Maybe some
workarounds in std or boost I don't know about ?..

Best Regards, Paul Sujkov
 
V

Victor Bazarov

let us have some class A

class A {};
with const static int variable var with
compile-time well-known value.

A member, I presume.

class A { public: static int var = WELL_KNOWN_VALUE; };
let us have some function f(), which
has return type of A.

A f();
and let us have some template struct with non-
type template parameter : template <int a> Z.

template<int a> struct Z {};

See, it wasn't that difficult, was it? So, why use so many English
words when you could have written it in C++?
having it all there is a
question : why we cannot write smth like this : Z< f()::var> z ? there
is no need in real call to function f - it can be not defined as all
(declared only), because all we need is a static const variable value.

I suppose it's because the Standard does not allow the use of :: with
an object, and because a function call cannot be part of integral const
expression.

What you could do, I suppose, is use

Z< std::tr1::result_of(f)::type::var > z;

AFAIUI, (see more of technical report in the Committee documents).
So why compiler does not let us to do so ? N.B. the one and only
operator that works in this example is sizeof, but I cannot see ant
real difference between sizeof and getting static const variable
value. And is there some way to avoid this error ? Maybe some
workarounds in std or boost I don't know about ?..

See above, but note that not all compilers have implemented TR1 yet.

V
 
P

psujkov

Thank you for the answer :)
class A {};
class A { public: static int var = WELL_KNOWN_VALUE; };
A f();
template<int a> struct Z {};
See, it wasn't that difficult, was it? So, why use so many English
words when you could have written it in C++?

I am not very familiar with google groups formatting rules, so I was
not sure would my code here be readable at all
What you could do, I suppose, is use
Z< std::tr1::result_of(f)::type::var > z;

Hmm...it doesn't work with my issue. result_of works with function,
not function call, but I need to work with function template with
return type instantiation depending on it's arguments. Look at the
example :

template <typename R, typename A1>
boost::mpl::vector<A1> GetArgs(R (*f) (A1));

template <typename R, typename A1, typename A2>
boost::mpl::vector<A1, A2> GetArgs(R (*f) (A1, A2));

Z< std::tr1::result_of( GetArgs(&f1) )::type::var > z // error :
function call in static
Z< std::tr1::result_of( &GetArgs )::type::var > z // error : result
type is undefined

So return type of my function depends on it's arguments. But I cannot
provide arguments to function while calling result_of. Even worse, I
cannot provide template with explicit types because there's no
possibility to obtain a signature from a function : I must use type
propogation (which works with function templates), but it is necessary
to provide template with real arguments for the compiler to be able to
propogate types...
AFAIUI, (see more of technical report in the Committee documents).

Haven't found anything usable yet :(
See above, but note that not all compilers have implemented TR1 yet.

I used boost::utility library : it shares result_of class with TR1

Any ideas ?..

Best Regards, Paul Sujkov
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top