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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:19:39+00:00 2026-05-14T14:19:39+00:00

I am trying to define a full specialization of std::basic_string< char, char_traits<char>, allocator<char> >

  • 0

I am trying to define a full specialization of std::basic_string< char, char_traits<char>, allocator<char> > which is typedef’d (in g++) by the <string> header.

The problem is, if I include <string> first, g++ sees the typedef as an instantiation of basic_string and gives me errors. If I do my specialization first then I have no issues.

I should be able to define my specialization after <string> is included. What do I have to do to be able to do that?

My Code:

#include <bits/localefwd.h>

//#include <string> // <- uncommenting this line causes compilation to fail

namespace std {
template<>
class basic_string< char, char_traits<char>, allocator<char> >
{
public:
    int blah() { return 42; }
    size_t size() { return 0; }
    const char *c_str() { return ""; }
    void reserve(int) {}
    void clear() {}
};
}

#include <string>
#include <iostream>

int main() {
    std::cout << std::string().blah() << std::endl;
}

The above code works fine. But, if I uncomment the first #include <string> line, I get the following compiler errors:

blah.cpp:7: error: specialization of ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ after instantiation
blah.cpp:7: error: redefinition of ‘class std::basic_string<char, std::char_traits<char>, std::allocator<char> >’
/usr/include/c++/4.4/bits/stringfwd.h:52: error: previous definition of ‘class std::basic_string<char, std::char_traits<char>, std::allocator<char> >’
blah.cpp: In function ‘int main()’:
blah.cpp:22: error: ‘class std::string’ has no member named ‘blah’

Line 52 of /usr/include/c++/4.4/bits/stringfwd.h:

  template<typename _CharT, typename _Traits = char_traits<_CharT>,
           typename _Alloc = allocator<_CharT> >
    class basic_string;

As far as I know this is just a forward delcaration of the template, NOT an instantiation as g++ claims.

Line 56 of /usr/include/c++/4.4/bits/stringfwd.h:

   typedef basic_string<char>    string;

As far as I know this is just a typedef, NOT an instantiation either.

So why are these lines conflicting with my code? What can I do to fix this other than ensuring that my code is always included before <string>?

  • 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-14T14:19:40+00:00Added an answer on May 14, 2026 at 2:19 pm

    You are only allowed to specialize a standard library if the specialization depends on a user-defined name with external linkage. char doesn’t meet this requirement and you are getting undefined behaviour.

    This is specified in 17.4.3.1 [lib.reserver.names]/1.

    The particular error that you are getting is because your implementation already instantiates the template that you are trying to specialize and, if you provide a specialization for any template, it must be before the template is ever instantiated with the parameters for which you want to provide the specialization.

    14.7.3 [temp.expl.spec]/6

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

Sidebar

Related Questions

I'm trying to do a simple custom runtime_error. I define the class: #include <string>
Im trying to define a dataTemplate for a business object in my wpf application
I'm trying to define a task that emits (using echo) a message when a
I'm trying to define a table to store student grades for a online report
I am trying to define a WCF contract that returns an interface, something like
I'm trying to dynamically define functions that call through to another function that takes
I am trying to figure out how to define a function that works on
I'm trying to generate a CSS class that will help me define a page
I'm trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a
Currently, I'm trying to make a function that sorts a vector full of fighters

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.