Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C++
Technical Blog

C++ Digraphs

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
8 Oct 2013CPOL2 min read 12.5K   2  
The digraphs I am writing about are sequences of characters which act as a stand in for other characters.

This is not as you might think, an article about implementing directed graphs in C++. The digraphs I am writing about are sequences of characters which act as a stand in for other characters. Digraphs and trigraphs exist in many languages, but I will be focusing on C++. The difference between digraphs and trigraphs is simple the number of characters, a digraph is 2 characters and a trigraph is 3 characters.

Digraphs are part of the language because in the past special characters were hard to input. This was generally because they were not on the keyboard, but in a few cases, they were not even in the code page!

So what use is this now, when modern keyboards have all the symbols we could want and we can use different encodings in source files?

Here is a screenshot of the C++ standard where it defines the allowed digraphs.

Digraphs in C++ Standard

C++ standard digraphs

The first column are backwards compatible digraphs for C. The next two columns are rather interesting.

For example. If you write the word or in a C++ source file, the compiler will replace that with ||. This means that the following code compiles and works.

//=============================================================================
int main() {
  bool a = false;
  bool b = true;
  std::cout << "a or b == " << (a or b) << std::endl;
  return 0;
}

Not only will this work for built in types, this works for user defined operators too. This is because the compiler simply substitutes the symbols.

Here is an example class and calling code which uses all the C++ digraphs.

Class

This is just a stub class which defines a lot of operators which do nothing.

//=============================================================================
class Example {
public:

  //===========================================================================
  // logic operators
  bool operator&&(const Example& t) {
    return true;
  }
  
  bool operator||(const Example& t) {
    return true;
  }
  
  bool operator!() {
    return true;
  }
  
  //===========================================================================
  // bitwise operators
  bool operator&(const Example& t) {
    return true;
  }
  
  bool operator|(const Example& t) {
    return true;
  }
  
  bool operator^(const Example& t) {
    return true;
  }
  
  bool operator~() {
    return true;
  }
  
  //===========================================================================
  // logic equals operators
  Example operator&=(const Example& t) {
    return t;
  }
  
  Example operator|=(const Example& t) {
    return t;
  }
  
  Example operator^=(const Example& t) {
    return t;
  }
  
  Example operator!=(const Example& t) {
    return t;
  }
  
};

Calling Code

//=============================================================================
int main() {
  bool a = true;
  bool b = true;
  if (a and b) {
    cout << "\"and\" is an operator." << endl;
  }
  if (false or b) {
    cout << "\"or\" is also an operator." << endl;
  }
  if (not false) {
    cout << "\"not\" is also an operator." << endl;
  }

  Example t_1, t_2;
  if (t_1 and t_2) {
    cout << "\"and\" even works for classes" << endl;
  }
  
  if (t_1 or t_2) {
    cout << "\"or\" even works for classes" << endl;
  }
  
  if (not t_1) {
    cout << "\"and\" even works for classes" << endl;
  }
  
  if (t_1 bitand t_2) {
    cout << "\"bitand\" also works for classes" << endl;
  }
  
  if (t_1 bitor t_2) {
    cout << "\"bitor\" even works for classes" << endl;
  }
  
  if (compl t_1) {
    cout << "\"compl\" even works for classes" << endl;
  }
  
  if (t_1 xor t_2) {
    cout << "\"xor\" even works for classes" << endl;
  }
  
  t_1 and_eq t_2;
  cout << "\"and_eq\" even works for classes" << endl;
  
  t_1 or_eq t_2;
  cout << "\"or_eq\" even works for classes" << endl;
  
  t_1 xor_eq t_2;
  cout << "\"xor_eq\" even works for classes" << endl;

  return 0;
}

This gives the output;

"and" is an operator.
"or" is also an operator.
"not" is also an operator.
"and" even works for classes
"or" even works for classes
"and" even works for classes
"bitand" also works for classes
"bitor" even works for classes
"compl" even works for classes
"xor" even works for classes
"and_eq" even works for classes
"or_eq" even works for classes
"xor_eq" even works for classes

This file can be found here.

This is an interesting feature, but is it useful?

I think it is, I think that the code;

if (not fail and ok) {
  // ...
}

Is more readable than;

if (!fail && ok) {
  // ...
}

However just because these are defined in the standard does not mean they can be used. These are largely considered a legacy part of the standard and there is limited support for it.

In particular Microsoft’s C++ compiler will not compile it. Both clang and g++ will compile it however, so if you are compiling using either of these you can use this nice feature.

Thanks for reading.

This article was originally posted at http://davidcorne.com/2013/10/08/c-digraphs

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
I am a C++ developer with a strong interest in Python, C#, and Qt. I work on a native C++ application which uses COM to call C# in order to use a WPF GUI.

While I develop an application using WPF exclusivly for windows, I am a big linux user. My favourite distro at the moment is Linux Mint, and I love the delights of the command line,.

If you've read something of mine and you enjoyed it, check out my blog.

I am also active on various other sites, listed below.

Coding Sites



  • BitBucket where I keep the majority of my projects.
  • GitHub where I have a few older projects. This includes my current long term project, I'm writing a book about design patterns in python. Find the repository here and blog posts about individual patterns here
  • Stackoverflow I'm not too active on stackoverflow, I'm more of a listener.
  • coderwall and coderbits two profile compilation websites.


Social Sites



Comments and Discussions

 
-- There are no messages in this forum --