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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:07:53+00:00 2026-06-08T10:07:53+00:00

I have this template: template <class T> v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args) { T *t

  • 0

I have this template:

template <class T>
v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args)
{
    T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
    if (t != NULL) t->volume(args[0]->NumberValue());
    return args.This();
}

I want to make it 100% dynamic, so I’m hoping to replace t->volume with a pointer (std::mem_fn?). The thing is, I can’t figure out from similar examples/questions how to retain the jsFunctionTemplate’s current type (it must be a v8::InvocationCallback)

typedef Handle<Value> (*InvocationCallback)(const Arguments& args);

So that it’s usage can still be:

audio->PrototypeTemplate()->Set("Volume", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio>));

I am not opposed to using even C++11 syntax.

  • 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-08T10:07:55+00:00Added an answer on June 8, 2026 at 10:07 am

    I don’t claim to fully understand the limitations which the v8 API imposes here, but if a template argument for the type is all right, then I hope you can use a pointer-to-member template argument for what you want to achieve here. The following self-contained and tested example, although without v8, should demonstrate how this can be done:

    #include <iostream>
    
    struct foo {
      void bar(int arg) {
        std::cout << "bar(" << arg << ") called" << std::endl;
      }
    };
    
    template<class T, void (T::*fun)(int)>
    void ptfWrapup(void* ptr, int arg) {
      (static_cast<T*>(ptr)->*fun)(arg);
    }
    
    int main() {
      foo f;
      ptfWrapup<foo, &foo::bar>(&f, 42);
    }
    

    Applied to your situation, and assuming NumberType to be the return value of that NumberValue call, or more importantly the type of the first argument of the functions you want to call, it would look something like this:

    template <class T, void (T::*fun)(NumberType)>
    v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args)
    {
        T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
        if (t != NULL) (t->*fun)(args[0]->NumberValue());
        return args.This();
    }
    
    … v8::FunctionTemplate::New(&jsFunctionTemplate<Audio, &Audio::volume>) …
    

    In contrast to the example above, this here is untested, but on the other hand closer to your code. Together they should make the idea crystal clear, I hope.

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

Sidebar

Related Questions

I have several very similar functions: v8::Handle<v8::Value> jsAudioPlay(const v8::Arguments &args) { Audio *audio =
I have non-template class with a templatized constructor. This code compiles for me. But
I have a templated container class that looks something like this: template <class ItemType>
I have this code inside a header (edited): template <int i> class A {};
I have this code: struct A{}; template<class T = A> struct B { void
I have code that looks like this: template<class T> class list { public: class
Suppose I have a function which looks like this: template <class In, class In2>
This is more of a design question. I have a template class, and I
Hallo I have this script: <? require(lib2/config.inc.php); require(lib2/tpl.class.php); require(lib2/db.class.php); require(lib2/um.class.php); $tpl = new template(templates,
I have a C++ class like this: class ConnectionBase { public: ConnectionBase(); template <class

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.