Discussion:
Returning 'const' values
(too old to reply)
David Chandler
2011-03-18 18:35:20 UTC
Permalink
In IDL I have defined the following value

const string INVALID = "INVALID";

which compiles to the following C++ code:
const CORBA::Char* INVALID = "INVALID";

and the following function

string foo();

which compiles to the following C++ code:
CORBA::Char* foo();

In the impl, I'd like, under certain circumstances to allow foo() to
return INVALID. This results in compiler warnings about const and non-
const types. However, the IDL compiler chokes on

const string foo();

which is what I'd really like to do (something that compiles into
const CORBA::Char* foo() )

What's the proper way to handle this?
Martin B.
2011-03-18 23:03:57 UTC
Permalink
Post by David Chandler
In IDL I have defined the following value
const string INVALID = "INVALID";
const CORBA::Char* INVALID = "INVALID";
and the following function
string foo();
CORBA::Char* foo();
In the impl, I'd like, under certain circumstances to allow foo() to
return INVALID. This results in compiler warnings about const and non-
const types. However, the IDL compiler chokes on
const string foo();
which is what I'd really like to do (something that compiles into
const CORBA::Char* foo() )
What's the proper way to handle this?
You cannot return a const string from a CORBA function.

Not only does your compiler choke on the construct, it should also crash
at runtime.

When you have a CORBA function returning a string, the contract btw. the
function and its caller is as follows:

* The function (resp. its stub code) promises to (dynamically) allocate
a "string" (via CORBA::string_alloc).
* The caller (resp. the skeleton code) promises to deallocate this
"string" after it's used it (via CORBA::string_free).

Therefore, you cannot fulfil this contract with a static string such as
your string constant.

To make your code work, you can possibly simply `return
CORBA::string_dup(INVALID);`.

cheers,
Martin
--
Stop Software Patents
http://petition.stopsoftwarepatents.eu/841006602158/
http://www.ffii.org/
Loading...