473,394 Members | 1,722 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,394 software developers and data experts.

C++/CLI : Portable operators?

I have a C++/CLI class I wish to have source compatible with regular
C++. I'm looking for a good way to handle the operators :

public value class SampleClass
{
//- C++/CLI
static bool operator==(SampleClass %, SampleClass %);

//- C++
//- friend bool operator==(const SampleClass &, const SampleClass
&);
//- bool operator==(const SampleClass &) const;

int value_; //- Just to have something
};

The types and the class declaration are easily handled by macros, but
the operators are a lot more hideous. I'd like the operator== in
C++/CLI to be in the class -- I want it reflected -- but static
operator== is just plain illegal in C++. The best I've got is :

//- Yuck.
#if defined(__cplusplus_cli)
#define VALUE_CLASS(name) public value struct name
#define OP_DECLARE(ret,name,p1,p2) \
static ret operator ## name(p1, p2)
#define OP_DEFINE(cls,ret,name,p1,p2) \
ret cls::operator ## name(p1 left, p2 right)

template<typename TYPE>
struct value_ref
{
typedef TYPE %_t;
};

#else
#define VALUE_CLASS(name) class name
#define OP_DECLARE(ret,name,p1,p2) \
friend ret operator ## name (p1, p2)
#define OP_DEFINE(cls,ret,name,p1,p2) \
ret operator ## name(p1 left,p2 right)

template<typename TYPE>
struct value_ref
{
typedef const TYPE &_t;
};
#endif

VALUE_CLASS(SampleClass)
{

OP_DECLARE(bool,==,value_ref<SampleClass>::_t,valu e_ref<SampleClass>::_t);

int value_;
};

OP_DEFINE(SampleClass,bool,==,value_ref<SampleClas s>::_t,value_ref<SampleClass>::_t)
{
return left.value_ == right.value_;
}

If you've got something cleaner, please let me know. Thanks!

David Hunt

Jan 23 '06 #1
1 1420
dc****@gmail.com wrote:
I have a C++/CLI class I wish to have source compatible with regular
C++. I'm looking for a good way to handle the operators :


I don't think you can have a single code base for managed and unmanaged
code. The syntax is way too different, even for trivial code like yours.
If you want to write any real-world application, you'll need to use
strings and containers, where differences are even more significant. How
would you handle std::string and System::String^ with a single code
base, or std::vector<T> and List<T^>^? With STL.NET it would be somewhat
easier, but there are just enough syntactical differences that you won't
easily be able to use a single code base.

I recommend that you wrap unmanaged code into managed classes, and
provide conversion functions between them (such as enum <-> enum class,
struct <-> value struct, and so on).

Tom
Jan 23 '06 #2

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

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
13
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
2
by: octangle | last post by:
Create enumerated values using the three highest bits as bit flags that define comparison operators. VERSION A: ---------- enum eCriteria { eCriteriaOne = 1, eCriteriaTwo = 2,
5
by: copx | last post by:
How portable are direct bit operations? Which ones are portable? I have never bothered learning such low-level stuff (I have an excuse: I am not a professional programmer), so I really don't know....
16
by: Noob | last post by:
Hello, I've rewritten a function (greater_or_equal) that relies on implementation-defined behavior and availability of exact-width integers, with the goal of making the new implementation...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.