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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:36:09+00:00 2026-06-02T18:36:09+00:00

Consider the following: struct A { typedef int foo; }; struct B {}; template<class

  • 0

Consider the following:

struct A {
  typedef int foo;
};

struct B {};

template<class T, bool has_foo = /* ??? */>
struct C {};

I want to specialize C so that C<A> gets one specialization and C<B> gets the other, based on the presence or absence of typename T::foo. Is this possible using type traits or some other template magic?

The problem is that everything I’ve tried produces a compile error when instantiating C<B> because B::foo doesn’t exist. But that’s what I want to test!


Edit:
I think ildjarn’s answer is better, but I finally came up with the following C++11 solution. Man is it hacky, but at least it’s short. 🙂

template<class T>
constexpr typename T::foo* has_foo(T*) {
  return (typename T::foo*) 1;
}
constexpr bool has_foo(...) {
  return false;
}
template<class T, bool has_foo = (bool) has_foo((T*)0)>
  • 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-02T18:36:10+00:00Added an answer on June 2, 2026 at 6:36 pm

    Another (C++03) approach:

    template<typename T>
    struct has_foo
    {
    private:
        typedef char no;
        struct yes { no m[2]; };
    
        static T* make();
        template<typename U>
        static yes check(U*, typename U::foo* = 0);
        static no check(...);
    
    public:
        static bool const value = sizeof(check(make())) == sizeof(yes);
    };
    
    struct A
    {
        typedef int foo;
    };
    
    struct B { };
    
    template<typename T, bool HasFooB = has_foo<T>::value>
    struct C
    {
        // T has foo
    };
    
    template<typename T>
    struct C<T, false>
    {
        // T has no foo
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Consider the following code template<typename T, int N> struct A { typedef T value_type;
Consider the following: typedef struct { int a; int b; int c; int d;
Consider the following two FFI structs: class A < FFI::Struct layout :data, :int end
Consider the following code: template<class T, class F> struct X {}; template<class T, class
Consider the following code: template<size_t head,size_t ... Dims> struct make_vec { typedef typename make_vec<Dims...>::type
Consider the following code struct foo { const int txt_len; const int num_len; char
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct
Please consider the following code: typedef struct { int type; } object_t; typedef struct
please consider the following code: typedef struct Person* PersonRef; struct Person { int age;

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.