function template specialization syntax for cpp file
I have a templated function declared in my .h and implemented in my .cpp:
//file.h
class FileReader
template <class T> void Read( T *aValue );
;
//file.cpp
template<class T>
void
FileReader::Read( T *aValue )
//implementation
To allow the implementation in my .cpp, I had
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
But trying to fix a doxygen issue, someone pointed me here that I should use
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
This indeed, fixes the doxygen issue, but it breaks my compilation at linking.
=>What is the correct syntax to specialize my function template in my .cpp and allow the function to be linked?
This other question seems to indicate that I should use my second version. But this article uses my first version.
c++ templates
add a comment |
I have a templated function declared in my .h and implemented in my .cpp:
//file.h
class FileReader
template <class T> void Read( T *aValue );
;
//file.cpp
template<class T>
void
FileReader::Read( T *aValue )
//implementation
To allow the implementation in my .cpp, I had
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
But trying to fix a doxygen issue, someone pointed me here that I should use
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
This indeed, fixes the doxygen issue, but it breaks my compilation at linking.
=>What is the correct syntax to specialize my function template in my .cpp and allow the function to be linked?
This other question seems to indicate that I should use my second version. But this article uses my first version.
c++ templates
1
@user463035818 I reopened the question, because OP correctly tries to explicitly instantiate the class template for a specific set of template arguments, but was misguided
– Piotr Skotnicki
Nov 14 '18 at 18:55
@PiotrSkotnicki thanks. I saw cpp and some tempalte stuff and didnt read carefully enough
– user463035818
Nov 14 '18 at 18:57
add a comment |
I have a templated function declared in my .h and implemented in my .cpp:
//file.h
class FileReader
template <class T> void Read( T *aValue );
;
//file.cpp
template<class T>
void
FileReader::Read( T *aValue )
//implementation
To allow the implementation in my .cpp, I had
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
But trying to fix a doxygen issue, someone pointed me here that I should use
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
This indeed, fixes the doxygen issue, but it breaks my compilation at linking.
=>What is the correct syntax to specialize my function template in my .cpp and allow the function to be linked?
This other question seems to indicate that I should use my second version. But this article uses my first version.
c++ templates
I have a templated function declared in my .h and implemented in my .cpp:
//file.h
class FileReader
template <class T> void Read( T *aValue );
;
//file.cpp
template<class T>
void
FileReader::Read( T *aValue )
//implementation
To allow the implementation in my .cpp, I had
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
But trying to fix a doxygen issue, someone pointed me here that I should use
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
This indeed, fixes the doxygen issue, but it breaks my compilation at linking.
=>What is the correct syntax to specialize my function template in my .cpp and allow the function to be linked?
This other question seems to indicate that I should use my second version. But this article uses my first version.
c++ templates
c++ templates
asked Nov 14 '18 at 18:45
David LevyDavid Levy
1279
1279
1
@user463035818 I reopened the question, because OP correctly tries to explicitly instantiate the class template for a specific set of template arguments, but was misguided
– Piotr Skotnicki
Nov 14 '18 at 18:55
@PiotrSkotnicki thanks. I saw cpp and some tempalte stuff and didnt read carefully enough
– user463035818
Nov 14 '18 at 18:57
add a comment |
1
@user463035818 I reopened the question, because OP correctly tries to explicitly instantiate the class template for a specific set of template arguments, but was misguided
– Piotr Skotnicki
Nov 14 '18 at 18:55
@PiotrSkotnicki thanks. I saw cpp and some tempalte stuff and didnt read carefully enough
– user463035818
Nov 14 '18 at 18:57
1
1
@user463035818 I reopened the question, because OP correctly tries to explicitly instantiate the class template for a specific set of template arguments, but was misguided
– Piotr Skotnicki
Nov 14 '18 at 18:55
@user463035818 I reopened the question, because OP correctly tries to explicitly instantiate the class template for a specific set of template arguments, but was misguided
– Piotr Skotnicki
Nov 14 '18 at 18:55
@PiotrSkotnicki thanks. I saw cpp and some tempalte stuff and didnt read carefully enough
– user463035818
Nov 14 '18 at 18:57
@PiotrSkotnicki thanks. I saw cpp and some tempalte stuff and didnt read carefully enough
– user463035818
Nov 14 '18 at 18:57
add a comment |
1 Answer
1
active
oldest
votes
The correct syntax depends on what you're actually trying to do. Adding the <> is NOT just a way to fix Doxygen---it substantially changes the meaning of the program!
The following are explicit instantiation definitions:
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler to instantiate the function template right then and there, and to emit symbols for the instantiations so that they can be linked to by another translation unit. This seems to be what you're actually trying to do.
The following are explicit specialization declarations:
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler that you're going to define your own specializations of the template for those particular template arguments. Thus, anyone who tries to call FileReader::Read<uint8_t> will NOT instantiate the primary template that you've already defined, but rather, look for a specialized definition. It doesn't look like that's what you're trying to do, but if it were, you would actually have to define those specializations at some point.
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
1
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306863%2ffunction-template-specialization-syntax-for-cpp-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The correct syntax depends on what you're actually trying to do. Adding the <> is NOT just a way to fix Doxygen---it substantially changes the meaning of the program!
The following are explicit instantiation definitions:
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler to instantiate the function template right then and there, and to emit symbols for the instantiations so that they can be linked to by another translation unit. This seems to be what you're actually trying to do.
The following are explicit specialization declarations:
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler that you're going to define your own specializations of the template for those particular template arguments. Thus, anyone who tries to call FileReader::Read<uint8_t> will NOT instantiate the primary template that you've already defined, but rather, look for a specialized definition. It doesn't look like that's what you're trying to do, but if it were, you would actually have to define those specializations at some point.
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
1
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
add a comment |
The correct syntax depends on what you're actually trying to do. Adding the <> is NOT just a way to fix Doxygen---it substantially changes the meaning of the program!
The following are explicit instantiation definitions:
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler to instantiate the function template right then and there, and to emit symbols for the instantiations so that they can be linked to by another translation unit. This seems to be what you're actually trying to do.
The following are explicit specialization declarations:
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler that you're going to define your own specializations of the template for those particular template arguments. Thus, anyone who tries to call FileReader::Read<uint8_t> will NOT instantiate the primary template that you've already defined, but rather, look for a specialized definition. It doesn't look like that's what you're trying to do, but if it were, you would actually have to define those specializations at some point.
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
1
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
add a comment |
The correct syntax depends on what you're actually trying to do. Adding the <> is NOT just a way to fix Doxygen---it substantially changes the meaning of the program!
The following are explicit instantiation definitions:
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler to instantiate the function template right then and there, and to emit symbols for the instantiations so that they can be linked to by another translation unit. This seems to be what you're actually trying to do.
The following are explicit specialization declarations:
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler that you're going to define your own specializations of the template for those particular template arguments. Thus, anyone who tries to call FileReader::Read<uint8_t> will NOT instantiate the primary template that you've already defined, but rather, look for a specialized definition. It doesn't look like that's what you're trying to do, but if it were, you would actually have to define those specializations at some point.
The correct syntax depends on what you're actually trying to do. Adding the <> is NOT just a way to fix Doxygen---it substantially changes the meaning of the program!
The following are explicit instantiation definitions:
template void FileReader::Read<uint8_t>( uint8_t * );
template void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler to instantiate the function template right then and there, and to emit symbols for the instantiations so that they can be linked to by another translation unit. This seems to be what you're actually trying to do.
The following are explicit specialization declarations:
template<> void FileReader::Read<uint8_t>( uint8_t * );
template<> void FileReader::Read<uint16_t>( uint16_t * );
They tell the compiler that you're going to define your own specializations of the template for those particular template arguments. Thus, anyone who tries to call FileReader::Read<uint8_t> will NOT instantiate the primary template that you've already defined, but rather, look for a specialized definition. It doesn't look like that's what you're trying to do, but if it were, you would actually have to define those specializations at some point.
answered Nov 14 '18 at 18:56
BrianBrian
64.6k796183
64.6k796183
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
1
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
add a comment |
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
1
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
Thanks for the clarification ! Now I need to fix my doxygen
– David Levy
Nov 14 '18 at 19:09
1
1
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
Next up: On the Confusing Nature of C++ Syntax.
– Passer By
Nov 14 '18 at 19:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306863%2ffunction-template-specialization-syntax-for-cpp-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
@user463035818 I reopened the question, because OP correctly tries to explicitly instantiate the class template for a specific set of template arguments, but was misguided
– Piotr Skotnicki
Nov 14 '18 at 18:55
@PiotrSkotnicki thanks. I saw cpp and some tempalte stuff and didnt read carefully enough
– user463035818
Nov 14 '18 at 18:57