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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:49:59+00:00 2026-05-10T21:49:59+00:00

Consider the following code: template <int dim> struct vec { vec normalize(); }; template

  • 0

Consider the following code:

template <int dim> struct vec {     vec normalize(); };   template <> struct vec<3> {     vec cross_product(const vec& second);     vec normalize(); };  template <int dim> vec<dim> vec<dim>::normalize() {     // code to normalize vector here     return *this; }  int main() {     vec<3> direction;     direction.normalize(); } 

Compiling this code produces the following error:

1>main.obj : error LNK2019: unresolved external symbol ‘public: struct vec<3> __thiscall vec<3>::normalize(void)’ (?normalize@?$vec@$02@@QAE?AU1@XZ) referenced in function _main

  • 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. 2026-05-10T21:50:00+00:00Added an answer on May 10, 2026 at 9:50 pm

    You can’t 🙂 What you want is to specialize the member functions instead:

    template <int dim> struct vec {     // leave the function undefined for everything except dim==3     vec cross_product(const vec& second);     vec normalize(); };  template<> vec<3> vec<3>::cross_product(const vec& second) {     // ... }  template <int dim> vec<dim> vec<dim>::normalize() {     // code to normalize vector here     return *this; } 

    Another, slightly more complicated solution is to use boost::enable_if:

    template <int dim> struct vec {     // function can't be called for dim != 3. Error at compile-time     template<int dim1>     typename boost::enable_if_c< dim == dim1 && dim1 == 3, vec<dim1> >::type      cross_product(const vec<dim1>& second) {         // ...     }     vec normalize();      // delegate to the template version     void without_params() {         // delegate         this->without_params<dim>();     }  private:     // function can't be called for dim != 3. Error at compile-time     template<int dim1>     typename boost::enable_if_c< dim == dim1 && dim1 == 3 >::type      without_params() {         // ...     }    };  template <int dim> vec<dim> vec<dim>::normalize() {     // code to normalize vector here     return *this; } 

    That will cause a compile time error if cross_product is called for any dim != 3. Note that that ‘trick’ only works for functions with parameters, since only then the template parameter can be auto-deduced. For cases without parameters, i have provided a function without_parameters above :).

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

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You do not need an intermediate string, you can create… May 11, 2026 at 8:20 am
  • added an answer One common example is where the struct is a member… May 11, 2026 at 8:20 am
  • added an answer You could try using negative lookbehind like that: (\<ct:([^\s\>]*)(.*?)(?<!-|=)\>) Matches… May 11, 2026 at 8:20 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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

Related Questions

Loading...

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.