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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:37:24+00:00 2026-06-06T02:37:24+00:00

I’m currently trying to make a set of conversion functions which, through one call,

  • 0

I’m currently trying to make a set of conversion functions which, through one call, can (attempt to) convert a JavaScript object (CefV8Value) into its C++ counterpart, with support for pointers.

Here are the conversion functions (pointer conversion at the end):

template<typename T>
T convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value) {};

// Explicit type conversion functions
#define V8VALUE_TO_CPP_CONVERSION(type) \
      template<> type \
      convert_v8value_to_cpp<type>(const CefRefPtr<CefV8Value> &value)

V8VALUE_TO_CPP_CONVERSION(CefRefPtr<CefV8Value>)
{
    return value;
}

V8VALUE_TO_CPP_CONVERSION(bool)
{
    return value->GetBoolValue();
}

V8VALUE_TO_CPP_CONVERSION(int)
{
    return value->GetIntValue();
}

V8VALUE_TO_CPP_CONVERSION(std::string)
{
    return value->GetStringValue().ToString();
}

V8VALUE_TO_CPP_CONVERSION(const char *)
{
    return value->GetStringValue().ToString().c_str();
}

V8VALUE_TO_CPP_CONVERSION(std::wstring)
{
    return value->GetStringValue().ToWString();
}

// HACKHACK: most VGUI functions take non-const wchar_t pointers, when they
// shouldn't
V8VALUE_TO_CPP_CONVERSION(wchar_t *)
{
    return (wchar_t*)value->GetStringValue().ToWString().c_str();
}

V8VALUE_TO_CPP_CONVERSION(const wchar_t *)
{
    return value->GetStringValue().ToWString().c_str();
}

V8VALUE_TO_CPP_CONVERSION(double)
{
    return value->GetDoubleValue();
}

V8VALUE_TO_CPP_CONVERSION(float)
{
    return value->GetDoubleValue();
}

//-----------------------------------------------------------------------------
// Purpose: converts a JS array to a C++ vector (of type T)
//-----------------------------------------------------------------------------
template<typename T>
std::vector<T> convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
{
    std::vector<T> vec;

    if(!value->IsArray())
        return vec;

    for(int i = 0; i < value->GetArrayLength(); ++i)
    {
        CefRefPtr<CefV8Value> element = value->GetValue(i);
        vec.push_back(convert_v8value_to_cpp<T>(element));
    }

    return vec; // hopefully move semantics will optimise this and not copy-construct
}

//-----------------------------------------------------------------------------
// Purpose: converts a JS object to a C++ pointer (where T is a pointer type)
//-----------------------------------------------------------------------------
template<typename T>
typename std::enable_if<std::is_pointer<T>::value, T>::type
    convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
{
    if(!value->IsObject())
        return NULL;

    CefRefPtr<CefV8Value> pTypeId = value->GetValue("__v8bind_typeid__");
    if(!pTypeId || !pTypeId->IsString())
        return NULL;

    CefRefPtr<CefV8Value> pPointerVal = value->GetValue("__v8bind_ptr__");
    if(!pPointerVal || !pPointerVal->IsInt())
        return NULL;

    if(pTypeId->GetStringValue().ToString() == typeid(T).name())
        return (T)pPointerVal->GetIntValue();

    return NULL;
}

And here’s the code that is using said pointer function:

WrapClass *pThis = convert_v8value_to_cpp<WrapClass*>(object);

Visual Studio complains that:

error C2668: 'convert_v8value_to_cpp' : ambiguous call to overloaded function
binding_test.cpp(106): could be 'Point *convert_v8value_to_cpp<WrapClass*>(const CefRefPtr<T> &)'
with
[
   WrapClass=Point,
   T=CefV8Value
]

binding_test.cpp(88): or       'std::vector<_Ty,_Ax> convert_v8value_to_cpp<WrapClass*>(const CefRefPtr<T> &)'
with
[
   _Ty=Point *,
   _Ax=std::allocator<Point *>,
   WrapClass=Point,
   T=CefV8Value
]

binding_test.cpp(31): or       'T convert_v8value_to_cpp<WrapClass*>(const CefRefPtr<CefV8Value> &)'
with
[
   T=Point *,
   WrapClass=Point
]

while trying to match the argument list '(CefRefPtr<T>)'
with
[
   T=CefV8Value
]

I don’t understand how the call is ambiguous (other than WrapClass * matches the first conversion function of T). However it also says that a possible call candidate is the std::vector conversion. How is this possible?

Many thanks in advance!

  • 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-06T02:37:26+00:00Added an answer on June 6, 2026 at 2:37 am

    Both of these:

    template<typename T>
    std::vector<T> convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
    { ... }
    
    template<typename T>
    typename std::enable_if<std::is_pointer<T>::value, T>::type
    convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
    { ... }
    

    are not function partial specialisations (which are not allowed anyway), but overloads, so together with the primary function template they’re all three ambiguous, because they only differ from each other in the return type.

    You want your function template convert_v8value_to_cpp to delegate to a static function (do_it(), say) in a class template convert_v8value_to_cpp_helper, because unlike function templates class templates can be specialised.

    Primary class template:

    template <typename T>
    struct convert_v8value_to_cpp_helper {}; // no do_it() here by intention
    

    Full specialisations:

    // Explicit type conversion functions
    #define V8VALUE_TO_CPP_CONVERSION(type, code) \
    template <> struct convert_v8value_to_cpp_helper< type > { \
      static T do_it() code \
    }
    
    V8VALUE_TO_CPP_CONVERSION(bool, {return value->GetBoolValue();});
    V8VALUE_TO_CPP_CONVERSION(int,  {return value->GetIntValue(); });
    V8VALUE_TO_CPP_CONVERSION(std::string,
    { return value->GetStringValue().ToString(); });
    V8VALUE_TO_CPP_CONVERSION(const char *,
    { return value->GetStringValue().ToString().c_str(); });
    V8VALUE_TO_CPP_CONVERSION(std::wstring,
    { return value->GetStringValue().ToWString(); });
    // HACKHACK: most VGUI functions take non-const wchar_t pointers, when they
    // shouldn't
    V8VALUE_TO_CPP_CONVERSION(wchar_t *,
    { return (wchar_t*)value->GetStringValue().ToWString().c_str(); });
    V8VALUE_TO_CPP_CONVERSION(const wchar_t *,
    { return value->GetStringValue().ToWString().c_str(); });
    V8VALUE_TO_CPP_CONVERSION(double, {return value->GetDoubleValue();});
    V8VALUE_TO_CPP_CONVERSION(float,  {return value->GetDoubleValue();});
    

    Specialisation for std::vector (incl. those with a custom allocator):

    template<typename T, typename A>
    struct convert_v8value_to_cpp< std::vector<T,A> > {
        static std::vector<T,A> do_it(const CefRefPtr<CefV8Value> &value)
        {
            std::vector<T,A> vec;
    
            if (!value->IsArray())
                return vec;
    
            for(int i = 0; i < value->GetArrayLength(); ++i)
            {
                CefRefPtr<CefV8Value> element = value->GetValue(i);
                vec.push_back(convert_v8value_to_cpp<T>(element));
            }
    
            return vec; // return value optimisation will kick in here
        }
    };
    

    And, finally, the specialisation for pointers:

    template<typename T>
    struct convert_v8value_to_cpp<T*> { // no need for enable_if
        static T* do_it(const CefRefPtr<CefV8Value> &value)
        {
            if (!value->IsObject())
                return nullptr; // don't use NULL in C++
    
            CefRefPtr<CefV8Value> pTypeId = value->GetValue("__v8bind_typeid__");
            if (!pTypeId || !pTypeId->IsString())
                return nullptr;
    
            CefRefPtr<CefV8Value> pPointerVal = value->GetValue("__v8bind_ptr__");
            if (!pPointerVal || !pPointerVal->IsInt())
                return nullptr;
    
            if (pTypeId->GetStringValue().ToString() == typeid(T).name())
                return (T*)pPointerVal->GetIntValue();
    
            return nullptr;
        }
    };
    

    These are now used in the real function template:

    template <typename T>
    T convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
    { return convert_v8value_to_cpp_helper<T>::do_it(value); }
    

    There’s no more ambiguity because there’s only one function template (but there might be ambiguity if partial template specialisation ordering fails to find a single best match, but that shouldn’t be the case here).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

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.