Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

5 views
Skip to first unread message

psu...@gmail.com

unread,
Mar 30, 2007, 10:16:41 AM3/30/07
to
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

Victor Bazarov

unread,
Mar 30, 2007, 11:22:29 AM3/30/07
to
psu...@gmail.com wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


psu...@gmail.com

unread,
Apr 2, 2007, 6:20:20 AM4/2/07
to
On 30 мар, 18:22, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

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

> 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

0 new messages