PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How to forward declare a template function in global or namespace with default argument?

 June 25, 2022     c++, compiler-errors, default-arguments, forward-declaration, templates     No comments   

Issue

template<typename T> void foo (T t, int i = 0); // declaration

int main () { foo(1, 0); } // error!! 

template<typename T> void foo (T t, int i = 0) {} // definition

Above is a minimal reproducible example for a larger problem, where many header files are involved. Attempting to forward declare with default parameter results in below compilation:

error: redeclaration of ‘template void foo(T, int)’ may not have default arguments [-fpermissive]

How to fix this?


Solution

A default argument, like int i = 0, is seen as a definition. Repeating it is therefore an ODR-violation.

Don't know exactly why, except that the standard explicitly says so

Each of the following is termed a definable item:
[...]
(1.6) a default argument for a parameter (for a function in a given > scope)
[...] No translation unit shall contain more than one definition of any definable item.

http://eel.is/c++draft/basic.def.odr

The solution is then to only have the default argument appear once, likely in the declaration (and not repeated in the definition).



Answered By - BoP
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing