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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:40:37+00:00 2026-05-27T09:40:37+00:00

I have the following template class and a (global) variable of its type: template

  • 0

I have the following template class and a (global) variable of its type:

template <typename ClassT>
struct ClassTester : public ClassT {
    typedef ClassT type;
};

ClassTester<int> *aaa;  // No error here

I would expect a compilation error because int cannot be derived from, but this compiles fine under Visual C++ 2010.

If I remove the pointer, I get the expected compilation error (int cannot be derived from):

ClassTester<int> bbb; // Error here

I wanted to use this class for SFINAE testing whether the given type is a class that can be derived from:

template <typename T>
struct CanBeDerivedFrom  {

    template <typename C>
    static int test(ClassTester<T> *) { }

    template <typename>
    static char test(...) { }

    static const bool value = (sizeof(test<T>(0)) == sizeof(int));
};

This, however, always reports true, even for primitive types such as int because of the above reason. Is this an expected/valid behavior of C++?

  • 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-27T09:40:37+00:00Added an answer on May 27, 2026 at 9:40 am

    I think it’s not possible entirely to get a class which is derivable through SFINAE (which includes also the cases of final class in C++11). The best thing which can be done is to have a SFINAE for finding if a type is a class and rely upon that.

    template<typename T>
    struct void_ { typedef void type; };
    
    template<typename T, typename = void>
    struct CanBeDerivedFrom {
      static const bool value = false;
    };
    
    template<typename T>
    struct CanBeDerivedFrom<T, typename void_<int T::*>::type> {
      static const bool value = true;
    };
    

    This metaprogram will find if the given type is class/union or not. demo.

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

Sidebar

Related Questions

I have the following class: template <typename T> class matrix { private: int _n;
I have the following pattern: template <int a, int b> class MyClass { public:
I have following class: template <size_t size> class Araye{ public: Araye(int input[]){ for (int
I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int>
I have the following class template<typename hi_t, typename lo_t> struct int_t { hi_t hi;
Say I have the following class: template<class T> struct A { static int value;
I have the following template method declared in my interface: class IObjectFactory { public:
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {

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.