Digraphs and trigraphs in C++

15 June, 2010 § Leave a comment

This is a guest blog post by A.J. Orians.

In C++ you can program using digraphs and trigraphs. They are character substitutions you can make that may be easier to type depending on your keyboard layout. For instance instead of the following code:

int main()
{
   int array[3];
   return 0;
}

Using digraphs and trigraphs it can be written as the following:

int main()
??<
   int array<:3:>;
   return 0;
??>

However Visual Studio will not compile using digraphs and trigraphs. But it is still important to know about them because you could be creating cross-platform code that may fail and it may not be easy to understand why. Take the following piece of code:

#include <vector>
using namespace std;

class A{};

int main()
{
   vector<::A> vect;
   return 0;
}

Under g++ it will fail to compile with the following error:

main.cpp: In function int main():
main.cpp:8: error: <:: cannot begin a template-argument list
main.cpp:8: note: <: is an alternate spelling for [. Insert whitespace between < and ::
main.cpp:8: note: (if you use -fpermissive G++ will accept your code)

The reason why is vect; which is an error. To fix this error just put a space between the < and the ::A.

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Digraphs and trigraphs in C++ at JAWS.

meta

Follow

Get every new post delivered to your Inbox.

Join 1,007 other followers