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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:20:39+00:00 2026-05-23T03:20:39+00:00

I’m trying to trigger a compile time error if the user of my library

  • 0

I’m trying to trigger a compile time error if the user of my library tries to instantiate a template with a type that is not appropriate. I’ve implemented:

template <typename T>
struct good_type { enum { value = false }; };

template <>
struct good_type<string> { enum { value = true }; };

template <>
struct good_type<int64_t> { enum { value = true }; };

template <typename T>
struct X
{
  BOOST_STATIC_ASSERT(good_type<T>::value);
};

int main(int argc, char** argv)
{
  X<string> x1; 
  X<int64_t> x2;
  X<float> x3; 
  return 0;
}

which works, but the message I get from gcc is a bit surprising:

error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>' 

Should I be using a different Boost macro? Is there a better way to do this?

Thanks!

  • 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-23T03:20:40+00:00Added an answer on May 23, 2026 at 3:20 am

    You can use boost::enable_if, along with typelist.

    Define a typelist which contains all the types which you want to support, and write some metafunction(s), to check if a given type exists in the list or not, and then pass the value which metafunction returns to enable_if, so as to enable/disable the class.


    Alright, I wrote a code for demo. Its not using boost::enable_if though (that is for you to experiment with).

    Here is the framework first:

    ////////////////////////////////////////////////////////////
    //framework
    
    struct null_type {};
    
    template<typename H, typename  T=null_type>
    struct typelist
    {
       typedef H Head;
       typedef T Tail;
    };
    
    template<typename T, typename TList> struct exists;
    
    template<typename T, typename Tail> 
    struct exists<T, typelist<T, Tail> >
    {
        static const bool value = true;
    };
    
    template<typename T, typename Head, typename Tail> 
    struct exists<T, typelist<Head, Tail> >
    {
        static const bool value = false || exists<T, Tail>::value;
    };
    
    template<typename T> 
    struct exists<T, null_type >
    {
        static const bool value = false;
    };
    
    template<bool>
    struct compile_time_error;
    
    template<>
    struct compile_time_error<true> {};
    

    —

    Now follows the testing code:

    //////////////////////////////////////////////////////////////
    //usage
    
    typedef typelist<int> t1;
    typedef typelist<short, t1> t2;
    typedef typelist<char, t2> t3;
    typedef typelist<unsigned char, t3> t4;
    
    typedef t4 supported_types;//supported_types: int, short, char, unsigned char
    
    template<typename T>
    struct X
    {
        compile_time_error<exists<T,supported_types>::value> unsupported_type_used;
    };
    
    int main() {
    
     //testing if exists<> work or not!
     cout <<(exists<int,supported_types>::value)<< endl;        //should print 1
     cout <<(exists<unsigned int,supported_types>::value)<<endl;//should print 0
     cout <<(exists<char,supported_types>::value)<< endl;       //should print 1
     cout <<(exists<long,supported_types>::value)<< endl;       //should print 0
    
     X<int> x1;   //okay - int is supported!
     //X<long> x2;  //error - long is unsupported! 
     return 0;
    }
    

    which compiles perfectly fine (ideone), and gives this output (for the cout statements):

    1
    0
    1
    0
    

    But if you uncomment the line X<long> x2; in the above code, it will not compile, since long is an unsupported type. And it gives this error, which is easy to read and understand (ideone):

    prog.cpp: In instantiation of ‘X’:
    prog.cpp:68: instantiated from here
    prog.cpp:56: error: ‘X::unsupported_type_used’ has incomplete type
    prog.cpp:38: error: declaration of ‘struct compile_time_error’

    Hope this helps you.


    Now you can write a class template called enable_if_supported which takes two type arguments: T and supported_types. You can derive your class from enable_if_supported as:

    template<typename T>
    struct X : enable_if_supported<T, supported_types>
    {
       //your code
    };
    

    This looks a bit clean. enable_if_supported class template now is defined in the framework section. See it here working : http://www.ideone.com/EuOgc

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.