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 8211263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:22:18+00:00 2026-06-07T10:22:18+00:00

I have a class T and I want to make this code not compile:

  • 0

I have a class T and I want to make this code not compile:

void PassByValue(T);

but allow all of these:

void PassByRefernce(T&);
void PassByPointer(T*);

Note: that I want it to be illegal (i.e. generates a compile error) to declare a function taking a T by value even if the function is never used or defined.


Edit: Note that the following is totally valid C++:

class T {
  public:
    T() {}
  private:
    // Prevent copy/assignment
    T(const T&);
    T& operator=(const T&);
};

void Fn(T);  // I want this line to error, even if the function is never used.

int main() { return 0; } 
  • 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-07T10:22:20+00:00Added an answer on June 7, 2026 at 10:22 am

    It’s not strictly possible (see below), the best you can do is make such function unusable/undefinable by making T uncopyable.

    It is done by deleting the copy ctor, like this:

    class T
    {
        T(const T&) =delete;
        T& operator=(const T&) =delete;
        .....
    };
    

    If you cannot use C++11, the same effect happens making those declaration as private:

    class T
    {
    public:
        .....
    private:
        T(const T&);
        T& operator=(const T&);
    };
    

    No implementation is required.

    In this way the class cannot receive a state from another instance, and hence the only way to pass to a function is by an indirection (via reference or pointer).


    Of course this will not “solve” (literally) the original question, since declaration of things like

    void func(T);
    

    are still possible. However it will not be possible to call that function, and hence the safety of T is granted.

    Unfortunately, for how the C++ language is today defined, no “clean” solution exist, since a code like

    struct A;
    void func(A);
    

    must compile.

    Although the code itself does nothing (it just declare some symbols) there is nothing in struct A; saying something about A semantics. To deny the declaration of func we must be inside the translation unit (CPP file) that contains or includes the full A declaration (so that we can know no copy is possible), that must – at that point – preceed the one of func. But this happens not necessarilly in all the translation units the above snippet is included.

    There are languages that can do that: think of D, just to remain in the “system langages domain”. But D -which allow a “use before fully declare”- doesn’t “include sources” but “import modules” (the full definitions are accessible to the compiler) supporting iterative translation (if something is not yet defined and defined later the compiler goes back to “refine” and repeats until it “converges” to consistent code, or rejects the code if it finds inconsistencies). And -because of its “import mechanism” and semantic/syntactic separation, cannot link to (most of) C++, but only accepts C compiled libraries or objs. (and require C to D translation of headers!)

    Will C++ translation passes evolve towards a D-like one? Hard to be said. Very unlikely while the C backward compatibility of C “inclusion model” must be retained.

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

Sidebar

Related Questions

I have a member variable that in a class that I want to make
I have style sheet with a class name changebackgroundcolor i want make change in
I have this class which I want to pass around Windows as LPARAM parameter.
I have a number of class, all with exactly the same interface. This interface
I am using this code to make a Simple Command: public class SimpleCommand :
I have the following code: #include <iostream> class Grandma { public: virtual void foo()
I have a template class, what I want to do are the following Make
I'm trying to compile the following code: class MyClass { def foo(): this.type =
I want a hard reference class in my Java code, but, of course, there
I have class and I want to initalize column that stores pictuer with type

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.