473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Mar 30 '07 #1
2 1958
ps*****@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 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++?
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).
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
Mar 30 '07 #2
On 30 อมา, 18:22, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Thank you for the answer :)
class A {};
class A { public: static int var = WELL_KNOWN_VALUE; };
A f();
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++?
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<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...
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

Apr 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
7
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can...
3
by: red floyd | last post by:
Once again, I'm fighting a port from the (allegedly standard compliant) VC7.1 to G++. VC compiles this, G++ doesn't. Am I allowed to pass the anonymous temporary returned by f() to a function...
19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
6
by: Immortal_Nephi | last post by:
This Run() function is function pointer. It contains three functions. How can you put const in Run() function? It should guard against modifying Run()'s function pointer array. Do you know what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.