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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:15:19+00:00 2026-05-12T07:15:19+00:00

So, I have a macro. // swap_specialize.hpp #include <algorithm> #ifndef STD_SWAP_SPECIALIZE #define STD_SWAP_SPECIALIZE( CLASSNAME

  • 0

So, I have a macro.

// swap_specialize.hpp
#include <algorithm>

#ifndef STD_SWAP_SPECIALIZE
#define STD_SWAP_SPECIALIZE( CLASSNAME )            \
    namespace std {                                 \
    template<> inline                               \
    void swap( CLASSNAME & lhs, CLASSNAME & rhs )   \
    { lhs.swap(rhs); } }
#endif

So then I have a class

// c.hpp
#include <vector>
#include "swap_specialize.hpp"
class C
{
    public:
        C();

        void swap(C& rhs)
        {
            data_.swap(rhs.data_);
        }
        C& operator=(C rhs)
        {
            rhs.swap(*this);
            return *this;
        }
    private:
        std::vector<int> data_;
}

STD_SWAP_SPECIALIZE(C)

Does the usage of a macro to specialize std::swap in this way follow coding conventions?

  • 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-05-12T07:15:19+00:00Added an answer on May 12, 2026 at 7:15 am

    I would say it’s OK if it increases readability. Judge yourself. Just my two cents: Specializing std::swap isn’t really the right way to do this. Consider this situation:

    my_stuff::C c, b;
    // ...
    swap(c, b);
    // ...
    

    This won’t find std::swap if you haven’t done using std::swap or something similar. You should rather declare your own swap in C‘s namespace:

    void swap(C &a, C &b) {
      a.swap(b);
    }
    

    Now, this will work also in the above case, because argument dependent lookup searches in the namespace of the class. Code swapping generic things where the type isn’t known should do it like this:

    using std::swap;
    swap(a, b);
    

    Regardless of the type, this will use the best matching swap, and fall-back to std::swap if there wasn’t a better matching one in the namespaces of a. Hard-coding the call to std::swap will cut too short on types that don’t specialize std::swap but rather decide to provide their own swap in their namespace.

    This is superious in another way: Imagine C is a template. You cannot specialize std::swap in this case. But just defining your own swap, that’s perfectly fine.

    template<typename T>
    void swap(C<T> &a, C<T> &b) {
      a.swap(b);
    }
    

    This is the way how the swap for std::string and other classes is implemented too.

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

Sidebar

Ask A Question

Stats

  • Questions 183k
  • Answers 183k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your sequence seems entirely backwards to me. Here's how I… May 12, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer Python buffers output when it detects that it is not… May 12, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer <html> <?php $where = ''; foreach($_GET as $key => $value)… May 12, 2026 at 4:38 pm

Related Questions

I have a chart in a Worksheet in Excel and I have a macro
I have a 'foreach' macro I use frequently in C++ that works for most
I have a macro which refreshes all fields in a document (the equivalent of
Say I have a macro, FOO(name), and some template class Bar<> that takes one

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.