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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:43:35+00:00 2026-06-12T03:43:35+00:00

Let us suppose that we have a function of n variables y = f

  • 0

Let us suppose that we have a function of n variables

y = f (x1, ..., xn) 

Such a function I would like to pass as an argument.

In Matlab the following construction using a handle is available:

function y=func(x)
y = sin(x(0)) * cos(x(1))  //Any definition, not important

p_func=@func; //Define handle

It is possible to use the handle as a parameter of another function:

y = function2(p_func, n);

where n represents a dimension…

How to rewrite this code using C++? We use a simple model with the function template

temmplate <typename T>
T func( const T *arg, const short n) {return sin(arg[0]) * cos(arg[1])};

where xi arguments are represented by the 1-dimensional array of n elements. The problem is that in this case it is not possible to use a pointer to the function template

template <class T>
static T ( *pfunc ) ( const T *arg, const short n )

only a specialization… Perhaps another model could be more appropriate…Thanks for your help…

Remark:
I know that a class template is useful

template  <typename T>
class Foo
{ 
    T func( const T *args, const short n);
};

and this construction works:

template <class T>
static T ( *pfunc ) ( const T *arg, const short n )

But it may not be used in the current model of the library (I can not affect this).

  • 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-12T03:43:36+00:00Added an answer on June 12, 2026 at 3:43 am

    C++ is a statically typed language. Every object in C++, whether a function pointer, or whatever, must have a specific type. And the type of a function pointer is based on the types of arguments that the function to be pointed to is given.

    A template is not an object, so you can’t get a pointer to one. You can get a pointer to an instantiation of a template. Using your func definition, func<int> is a function that takes a const int* and a short. You can get a pointer to func<int>. But func is a template; you can’t get a pointer to a template.

    That’s why C++ programs often throw functors around instead of function pointers. Functors can have a template operator() method, so you can call them as though you were passing around template functions. But since you say that you have to use a function pointer, there’s not much that can be done.

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

Sidebar

Related Questions

Suppose that I have a request handler that accepts an argument: key And let
Let's suppose I have a css class named foo. I would like to have
The question is simple, let's suppose that I have an array like: $array =
Suppose I have class Function , whose instances are callables that take one argument.
Let's suppose I want to create a javascript class/object/function which have a method that
Let's suppose I have a C extension function that does something that is completely
Let's suppose that I have 2 processes in Erlang, and each process has a
First suppose that I have an abstract class, let's call it AbstractClass. Suppose public
Let's suppose that we have poor MVC framework without modules support. Our aim is
Let's say we have a function that changes a password for a user in

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.