Connecting Tech Pros Worldwide Forums | Help | Site Map

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

psujkov@gmail.com
Guest
 
Posts: n/a
#1: Mar 30 '07
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 aZ. having it all there is a
question : why we cannot write smth like this : Z< f()::varz ? 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


Victor Bazarov
Guest
 
Posts: n/a
#2: Mar 30 '07

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


psujkov@gmail.com wrote:
Quote:
let us have some class A
class A {};
Quote:
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; };
Quote:
let us have some function f(), which
has return type of A.
A f();
Quote:
and let us have some template struct with non-
type template parameter : template <int aZ.
template<int astruct Z {};

See, it wasn't that difficult, was it? So, why use so many English
words when you could have written it in C++?
Quote:
having it all there is a
question : why we cannot write smth like this : Z< f()::varz ? 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).
Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


psujkov@gmail.com
Guest
 
Posts: n/a
#3: Apr 2 '07

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


On 30 อมา, 18:22, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Thank you for the answer :)
Quote:
class A {};
class A { public: static int var = WELL_KNOWN_VALUE; };
A f();
template<int astruct Z {};
Quote:
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
Quote:
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<A1GetArgs(R (*f) (A1));

template <typename R, typename A1, typename A2>
boost::mpl::vector<A1, A2GetArgs(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...
Quote:
AFAIUI, (see more of technical report in the Committee documents).
Haven't found anything usable yet :(
Quote:
See above, but note that not all compilers have implemented TR1 yet.
I used boost::utility library : it shares result_of class with TR1
Quote:
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Any ideas ?..

Best Regards, Paul Sujkov

Closed Thread


Similar C / C++ bytes