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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:41:51+00:00 2026-06-18T19:41:51+00:00

Background I have some generic code for persistence that uses boost::variant to store multiple

  • 0

Background

I have some generic code for persistence that uses boost::variant to store multiple types. While outputting values, I had to write a converter function protected_backslash_n, which does nothing, in the default case, but return the same value back.

In the specialised case where the template parameter is a std::string I use boost::regex_replace() to search for \n and replace it with \\n.

Question

The code works fine, but it would be nice if I could get rid of the extra copy in the generic case, due to the return-by-value.

Is there a way to do this while allowing the specialised std::string version to work fine?

I tried changing the return value to T const&, but then the specialised version won’t match up.

Error

GetPipedValues.hpp:15:21: error: template-id ‘protect_backslash_n<std::string>’ for ‘std::string pitbull::protect_backslash_n(const string&)’ does not match any template declaration

Code

template<typename T>
inline T protect_backslash_n( T const& orig )
{
    return orig;  // BAD: extra copy - how do I get rid of this?
}

template<>
inline std::string protect_backslash_n<std::string>( std::string const& orig )
{
    boost::regex expr("(\\n)");
    std::string  fmt("(\\\\n)");
    return boost::regex_replace(
        orig, expr, fmt, boost::match_default | boost::format_all
    );
}
  • 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-18T19:41:53+00:00Added an answer on June 18, 2026 at 7:41 pm

    Don’t make it a template, but just an overload. The compiler will choose that function over a template instantiation, if the parameter matches.

    std::string protect_backslash_n( std::string const& orig);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that starts a background process for search in my WPF
I have some code that will change the background color of a specific label
let's say that I have some <TR style = background-color : red ;> and
Some background: we have a windows application (c#) that locate in the system try.
I have a background thread and the thread calls some methods that update the
I'm having issues with some code I'm working on. I have a generic function
I have this situation: there is some generic movieclip with some color in background
I have Some Background image in to My Application. That background Image also contain
First some background: I already have a Result class that I've implemented to carry
I have some HTML Code: <html> <head> <title>css test</title> <style type=text/css> .box{width:100%;float:left;background:red} </style> </head>

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.