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

  • Home
  • SEARCH
  • 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 8284739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:13:14+00:00 2026-06-08T11:13:14+00:00

I am writing a TMP to count the number of elements passed to a

  • 0

I am writing a TMP to count the number of elements passed to a struct as template parameters using variadic templates. This is my code:

template<class T, T... t>
struct count;

template<class T, T h, T... t> 
struct count<T, h, t...>{
 static const int value = 1 + count<T, t...>::value;
};

template<class T>
struct count<T>{
 static const int value = 0;
};

template<>
struct count<std::string, std::string h, std::string... l>{
 static const int value = 1 + count<std::string, l...>::value;
};

template<>
struct count<std::string>{
 static const int value = 0;
};

int main(){
 std::cout << count<int, 10,22,33,44,56>::value << '\n';
 std::cout << count<bool, true, false>::value << '\n';
 std::cout << count<std::string, "some">::value << '\n';
 return 0;

}

I get an error on the third instantiation of count with std::string because g++ 4.7 tells me error: ‘class std::basic_string<char>’ is not a valid type for a template non-type parameter. Any workaround for this?

  • 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-08T11:13:16+00:00Added an answer on June 8, 2026 at 11:13 am

    The problem is not the type std::string but the literal "some" in your call

    std::cout << count<std::string, "some">::value << '\n';
    

    Unfortunately, it is not possible to pass a string or floating point literal to a template as is also written in this answer or in that one.

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

Sidebar

Related Questions

I find myself writing this class often in my python code when I need
I'm writing my own template list class with iterator, and have this error((( compiler:GCC
Writing a python program, and I came up with this error while using the
I'm writing this in JAVA : stmt.executeQuery(SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS
I am writing a template class for an array of objects, call it arrayobjclass,
I'm writing a paperclip processor that class ffmpeg using the Paperclip.run command. Currently, that
I started writing this code a few days ago. I saw a question on
Writing Classic ASP code in JScript has a lot going for it: more humane
Writing documentation in html requires some code examples. What to do with characters that
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using

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.