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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:31:28+00:00 2026-05-31T04:31:28+00:00

I want to define a function template<typename T> T constCast(const ScriptVar_t& s); . Depending

  • 0

I want to define a function template<typename T> T constCast(const ScriptVar_t& s);. Depending on T, I want to have different definitions. (ScriptVar_t is a class but details are not important here in this context.)

The conditions on T aren’t as simple as specific types, they are all somewhat more complicated static boolean expressions. I.e. I have a list of expressions ext1..extN and for each, I have a definition of that function. And I want to have them checked in that order and the definition of the first matching expression should be used. If all of them fail, I want to get a compiler error.

Right now, I just have 2 definitions and my code looks like this (this is a full test case, the relevant code is marked):

#include <boost/type_traits.hpp>

enum {
    SVT_INT,
    SVT_FLOAT,
    SVT_BASEOBJ,
    SVT_CUSTOMVAR
};

struct BaseObject {};
struct CustomVar {};

template<typename T> struct GetType;
template<> struct GetType<int> { static const int value = SVT_INT; };
template<> struct GetType<float> { static const int value = SVT_FLOAT; };
template<> struct GetType<BaseObject> { static const int value = SVT_BASEOBJ; };

template<bool> struct GetType_BaseCustomVar;
template<> struct GetType_BaseCustomVar<true> {
    struct Type { static const int value = SVT_CUSTOMVAR; };
};
template<typename T> struct GetType : GetType_BaseCustomVar<boost::is_base_of<CustomVar,T>::value>::Type {};

struct ScriptVar_t;
template<typename T> T CastScriptVarConst(const ScriptVar_t& s);

struct ScriptVar_t {
    operator int() const { return 0; }
    operator float() const { return 0.0f; }
    operator BaseObject() const { return BaseObject(); }
    template<typename T> T* as() const { return NULL; }

    template <typename T> T castConst() const { return CastScriptVarConst<T>(*this); }
};

// *** relevant code starts here

template<typename T> T CastScriptVarConst(const ScriptVar_t& s);

template<bool> struct CastScriptVar1;
template<typename T> struct CastScriptVar1_IsSimpleType {
    static const bool value = GetType<T>::value < SVT_BASEOBJ;
};
template<> struct CastScriptVar1<true> {
    template<typename T> static T castConst(const ScriptVar_t& s, const T& /*dummy*/) { return (T) s; }
};

template<bool> struct CastScriptVar2;
template<typename T> struct CastScriptVar2_IsCustomVar {
    static const bool value = boost::is_base_of<CustomVar,T>::value;
};
template<> struct CastScriptVar2<true> {
    template<typename T> static T castConst(const ScriptVar_t& s, const T& /*dummy*/) { return *s.as<T>(); }
};

template<> struct CastScriptVar1<false> {
    template<typename T> static T castConst(const ScriptVar_t& s, const T& /*dummy*/) {
        return CastScriptVar2<CastScriptVar2_IsCustomVar<T>::value>::castConst(s, T());
    }
};
template<typename T> T CastScriptVarConst(const ScriptVar_t& s) {
    return CastScriptVar1<CastScriptVar1_IsSimpleType<T>::value>::castConst(s, T());
}

int main() {
    ScriptVar_t v;
    v.castConst<int>();
    v.castConst<CustomVar>();
}

I came up with this after a few tries until it works.

(As you can see from the code, the two expressions are GetType<T>::value < SVT_BASEOBJ and boost::is_base_of<CustomVar,T>::value. If both are false, the compiler should throw an error. But this is only an example for my question.)

I wonder if there is a somewhat more clean solution for this code.


For reference, I am playing with it here. And right now, I have again a somewhat different solution to all the other solutions here.

  • 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-31T04:31:30+00:00Added an answer on May 31, 2026 at 4:31 am

    If I understood correctly, I’d use a look-up table for the
    cast-functors and a meta-function to calculate the offset into the
    table.

    An alternative would be to use a type based look-up table consisting
    of tags and functors. pick_cast would then choose the right tag
    instead of an int. This might be easier to read if the decision
    table becomes large.

    #include <boost/type_traits.hpp>
    #include <boost/mpl/int.hpp>
    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/if.hpp>
    #include <boost/mpl/at.hpp>
    
    struct BaseObject {};
    struct CustomVar {};
    
    namespace mpl = boost::mpl;
    
    struct ScriptVar_t {
        operator int() const { return 0; }
        operator float() const { return 0.0f; }
        operator BaseObject() const { return BaseObject(); }
        template<typename T> T* as() const { return NULL; }
        template <typename T> T castConst() const;
    };
    
    struct default_cast {
      template<typename T>
      T operator()(const ScriptVar_t& s) const { return (T) s; }
    };
    
    struct base_cast {
      template<typename T>
      T operator()(const ScriptVar_t& s) const { return *s.as<T>(); }
    };
    
    typedef mpl::vector< default_cast, base_cast > casts;
    
    enum {
      DEFAULT = 0,
      BASE,
      END_OF_ENUM
    };
    
    // pick the right cast for T
    template<typename T>
    struct pick_cast {
      typedef typename mpl::if_< typename boost::is_base_of<CustomVar,T>::type,
                                 mpl::int_<BASE>, mpl::int_<DEFAULT> >::type type;
    };
    
    template <typename T> T ScriptVar_t::castConst() const { 
      typedef typename mpl::at<casts, typename pick_cast<T>::type>::type func;
      return func().template operator()<T>(*this);
    }
    
    int main() {
        ScriptVar_t v;
        v.castConst<int>();
        v.castConst<CustomVar>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a function using template template parameters (I just want to
I want to define a JavaScript class, Foo. Foo = function(value){ this.value = value;
I'm trying to create a function: template <typename T> void doIt( T*& p )
I would like to define a template function but disallow instantiation with a particular
I have a class template Foo<T> . I'd like to implement a non-member function
I have a dilemma. Suppose I have a template class: template <typename ValueT> class
This needs only work in g++. I want a function template<typename T> std::string magic();
I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I want to define record with procedure or function. Can you help with syntax?
I want to define a constant in objective-c. Previously I had the following function:

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.