diff --git a/CHANGES b/CHANGES
index 62675cb9d206c495707df577aef432cd27d194aa..7a7c578319ceda9aa100de5e092f75c220350242 100644
--- a/CHANGES
+++ b/CHANGES
@@ -195,6 +195,10 @@ o Support for the ", ##__VA_ARGS__" cpp feature.
   happen if you pass an empty argument, nor does it happen if the
   token preceding ‘##’ is anything other than a comma.
 
+o The define __COUNTER__ has been added. It is a unique integer
+  value, the first time the macro is expaded it will be 1, the next time
+  2 etc.
+
 o The preprocessor can now be run with a cpp prefix feature.
 
   This is currently used by the precompiler to avoid two levels of
diff --git a/src/cpp.c b/src/cpp.c
index 78f919230b5cdde87689f2595d8e9f1766460d7b..872798c16f47ba8fa94390738d8a2cb850a3af99 100644
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -2924,6 +2924,20 @@ static void insert_current_minor(struct cpp *this,
   string_builder_sprintf(tmp, " %d ", this->compat_minor);
 }
 
+/*! @decl int(1..) __COUNTER__
+ *! This define contains a unique counter (unless it has been expanded
+ *! Inte.NATIVE_MAX times) represented as an integer.
+ *!
+ */
+static void insert_current_counter(struct cpp *UNUSED(this),
+				 struct define *UNUSED(def),
+				 struct define_argument *UNUSED(args),
+				 struct string_builder *tmp)
+{
+  static int counter = 0;
+  string_builder_sprintf(tmp, " %d ", ++counter);
+}
+
 /*! @decl constant __MAJOR__
  *!
  *! This define contains the major part of the current Pike version,
@@ -3429,6 +3443,8 @@ void f_cpp(INT32 args)
   do_magic_define(&this,"_Pragma",insert_pragma)->args = 1;
   simple_add_define(&this, "static_assert", "_Static_assert");
 
+  do_magic_define(&this,"__COUNTER__",insert_current_counter);
+
   /* These are Pike extensions. */
   do_magic_define(&this,"__DIR__",insert_current_dir_as_string);
   do_magic_define(&this,"__VERSION__",insert_current_version);