Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:41:29+00:00 2026-06-17T17:41:29+00:00

I tried to write an operator that can be used both as a prefix

  • 0

I tried to write an operator that can be used both as a prefix and a postfix operator

#include <iostream>
#include <utility>

struct B { 
  // ...
};

template<typename ...T>
void operator++(B, T...) {
  std::cout << ((sizeof...(T) == 0) ? "prefix" : "postfix") << std::endl;
}

int main() {
  B b;
  b++;
  ++b;
}

GCC compiles and works fine with this, but clang says

main.cpp:9:24: error: parameter of overloaded post-increment operator must have type ‘int’ (not ‘T…’)

void operator++(B, T...) {

Who is right?


Thanks to anyone who helped me understand GCC’s behavior. I filed a new Clang bug report:

http://llvm.org/bugs/show_bug.cgi?id=14995

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:41:30+00:00Added an answer on June 17, 2026 at 5:41 pm

    ORIGINAL ANSWER: (not deleted as it may contain useful information)

    I would say it all boils down to whether an overloaded operator template is considered to be an overloaded operator or not. Logically, I would like to think this is not the case, and Clang is wrong: I believe the template should be first selected as a candidate for overload resolution based on name and signature compatibility, then instantiated, then (possibly) selected. The way I see it, only after instantiation the compiler should check whether the resulting function has the proper amount of arguments or not.

    But that is only my opinion. Per § 13.5.7/1 about overloading of the postfix operator ++:

    “If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int), it defines the postfix increment operator ++ for objects of that type”

    The Standard does not seem to clarify whether a function template shall be considered as a function for what concerns the restrictions on the signature of legal operator overloads (at least, I could not find any sentence that solves this ambiguity). As long as this is true, this question can hardly be given a clear answer, and we are left with opinions.

    But I would like to mention another relevant aspect of the matter: consistency.

    Although it is true that the code in the question’s text does not compile on Clang, the following does:

    template<typename... Ts>
    int operator + (X x1, Ts... args)
    {
        return 0;
    }
    

    I do not see any conceptual difference between the two cases: if the signature of the operator overload is to be checked before instantiation, then the above definition should not compile either. If this is not the case, then the code in the question’s text should compile.

    So the answer in my opinion is that either GCC is right, or they are both wrong.

    UPDATE:

    As @JesseGood and @SethCarnegie correctly point out, per 14.7/4:

    “A specialization is a class, function, or class member that is either instantiated or explicitly specialized.”

    Moreover, per 14.6/8:

    “No diagnostic shall be issued for a template for which a valid specialization can be generated.”

    Thus, it seems Clang is indeed wrong and no compilation error shall be produced for the operator function template in the question’s text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write bool-conversion operator for std::bitset I tried: template<size_t size> operator
I tried to write a function that calculates a hamming distance between two codewords
I tried to write a program that uses threads, but couldn't understand the o/p.
I've tried to write a function that will take an array of different amounts
So basically I want to write a function that can be written like this:
I'm trying to write a conversion operator function template in a class and running
I tried to write a function to check key code with JavaScript. It's working
I tried to write a script to list all files in directories and subdirectories
I tried to write a Neural Network system, but even running through simple AND/OR/NOR
I tried to write a Taylor series expansion for exp(x)/sin(x) using fortran, but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.