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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:15:28+00:00 2026-05-28T08:15:28+00:00

Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);

  • 0

Consider following code:

main.cpp:

#include <iostream>
typedef void ( * fncptr)(void);
extern void externalfunc(void);

template<void( * test)(void)>
class Bar
{
public:
    Bar() { test(); }
};

void localfunc()
{
    std::cout << "Hello World" << std::endl;
}
constexpr fncptr alias = localfunc;
extern fncptr externalAlias;
int main(int argc, char* argv[])
{
    Bar<localfunc> b;
    Bar<alias> b2; // This wouldn't compile if alias wasn't constexpr
    Bar<externalfunc> b3;
//  Bar<externalAlias> b4;

    return 0;
}

and external.cpp:

#include <iostream>

typedef void ( * fncptr)(void);

void externalfunc()
{
    std::cout << "Hello external world" << std::endl;
}

fncptr externalAlias = externalfunc;

Now the problem is I need something like the 4th line in the main func to work. I get those function declarations from an external C library so I cannot touch those. Currently the 4th line does not compile. gcc 4.6 says “it must be the address of a function with external linkage”. In fact it does also say this if you make alias not constexpr, so the actual meaning (I think) should be interpreted as: “I don’t know for 100% sure the function address you’re giving me is constant which I need for instantiating this template”. Is there any way around this as i cannot declare externalalias as constexpr in main.cpp?

Before you come with alternate solutions: I am already trying to get this work by just passing the function pointers through the constructor and save them locally but I am still interested if I could get make the template version to work :).

  • 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-28T08:15:29+00:00Added an answer on May 28, 2026 at 8:15 am

    The names alias and externAlias are not functions! They are pointers to functions and as such mutable. You cannot use mutable objects as template arguments because the template arguments need to be resolved at compile time. There are two things you can do, however:

    1. You can create a wrapper calling the potentially aliased function using whatever name you choose to give the wrapper. This can also conveniently adjust the type if there is some level of difference between the function signatures.
    2. You can use a constant pointer to function pointer as template argument: the usual one more level of indirection assumption applies here, too.

    All this said, what are you actually trying to achieve? Are you sure you don’t just want something like std::function<void(void)>? I realize that this is used for type erasure while you seem to have the opposite target and turn function pointers into unique types. However, the way you go about it seems that this isn’t really what you intend to do.

    Here is an example of how the second option could look like:

    #include <iostream>
    
    extern void f(int);
    extern void g(int);
    
    void (*falias)(int) = f;
    void (*galias)(int) = g;
    
    template <void (**alias)(int)>
    struct foo
    {
        void bar() { (*alias)(17); }
    };
    
    void f(int x) { std::cout << "f(x)=" << x << "\n"; }
    void g(int x) { std::cout << "g(x)=" << x << "\n"; }
    
    int main()
    {
        foo<&falias>().bar();
        foo<&galias>().bar();
    }
    

    The fact that f() and g() end up being implemented in the same translation unit is entirely immaterial: you can move them somewhere else without any problems.

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

Sidebar

Related Questions

Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args)
Consider the following code:- class Name { {System.out.println(hi);} public static void main(String[] args) {
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct

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.