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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:35:03+00:00 2026-06-11T20:35:03+00:00

I am trying to write a template like this. template <char c,typename Options, basic_string<class,class,class>

  • 0

I am trying to write a template like this.

template <char c,typename Options, 
   basic_string<class,class,class> Options::* member> 
   struct option_string;

However I keep getting a compilation error.
I would like to know how to write a template that takes three options: a char, any class and a pointer to a member of that class which must be a basic_string specialization.
I want to avoid writing a template for each specialization of basic_string.

My goal is to created a generic command line option library.
The following is how I would like to use it.

class Options : public Command_Line_Options_Parser<Options> {
public:
  int integer = 0;
  float real_number = 0.0;
  bool boolean = false;
  bool make_true = false;
  bool make_false = true;
  std::string string;
  typedef Options O;
  typedef Command_Line_Options<
            option_int<'i',&O::integer>,
            option_float<'f',&O::real_number>,
            option_bool<'b',&O::boolean>,
            option_true<'t',&O::make_true>,
            option_false<'t',&O::make_true>,
            option_string<'s',&O::string>
           >
  option_list;
};

int main(int argc,char**argv ) {

  Options options;

  options.parse(argc,argv);
  std::cout << "integer : " << options.integer <<endl;
  std::cout << "real_number : " << options.real_number <<endl;
  std::cout << "boolean : " << options.boolean <<endl;
  std::cout << "make_true : " << options.make_true <<endl;
  std::cout << "make_false : " << options.make_false <<endl;
  std::cout << "string : " << options.string <<endl;
};

I would prefer for option_string to accept all specializations of basic_string instead of just std::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-06-11T20:35:04+00:00Added an answer on June 11, 2026 at 8:35 pm

    You won’t get around having to specify the return type directly. And in that case, why bother with the basic_string class directly? Just take a typename String and be done:

    template<char C, class Options, class String, String Options::* Member>
    struct option_string;
    

    And if you still want to make sure that String is indeed a specialization of basic_string, well, take this little helper and static_assert it:

    #include <type_traits>
    #include <string>
    
    template<class T>
    struct is_basic_string : std::false_type{};
    
    template<class Ch, class Tr, class Al>
    struct is_basic_string<std::basic_string<Ch,Tr,Al>> : std::true_type{};
    
    // in 'option_string'
    static_assert(is_basic_string<String>::value,
        "Data member must be a 'basic_string' specialization.");
    

    Since there doesn’t seem to be a need to pass the member pointer as a template argument, I suggest just passing it in the constructor:

    template<char C, class Option, class String>
    struct option_string{
      static_assert(is_basic_string<String>::value,
          "Data member must be a 'basic_string' specialization.");
      typedef String Option::*member_type;
    
      option_string(member_type m, ...) : member(m), ... { ... }
    private:
      member_type member;
    };
    
    template<char C, class Option, class String>
    option_string<C, Option, String> make_option_string(String Option::*member, ...){
      return {member, ...};
    }
    
    // in code:
    auto os = make_option_string<'H'>(&some_type::a_string_member);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write non-member operator function templates like: #include <utility> template < typename
I am trying to write a template class in C++ and getting this strange
I have a template base class like this: template<typename T, std::size_t Size> class VectorT
I'm trying to write a string looking like this using go's template system: (p1,
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
noob here still experimenting with templates. Trying to write a message processing class template
I'm trying to write code that uses a member typedef of a template argument,
I'm trying to write a new emacs mode for a new template c-like language,
I'm trying to write something like: // I don't know how this particular syntax
I am trying to instantiate a template externaly however I would like o use

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.