Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is my understanding one of the benefits of C++ modules is speedier builds I attempted to test this but am confused by the result My test consisted of the following two files I compiled the source file and observed the #pragma message generated in the exported function foobar as expected I then made a trivial change in the source as shown below which does not alter the call to the exported function Then upon recompiling the source I expected not to see the above mentioned #pragma message stating foobar is being compiled However it was in fact displayed by the compiler presumably indicating the exported module was once again being compiled for no good reason which I can see So my question is am I doing something incorrectly in order to obtain the often stated speedier builds Thank You Kindly - Cheerio

original source file:
import Foobar;
int main()
{
	foobar(159);
}

module file:
export module Foobar;
export
template<typename valueType>
void foobar(valueType x)
{
#pragma message("compiling exported foobar")
}

first compile pragma message output:
compiling exported foobar

source file after insignificant change:
import Foobar;
int main()
{
	foobar(159);
	int x = 357;
}

second compile pragma message output:
compiling exported foobar


What I have tried:

The above explains what I tried This "What have you tried?" requirement is a nuisance
Posted
Updated 1-Oct-21 9:22am

1 solution

Your Foobar module is a template, and a template cannot be compiled on its own. The code for main changed, so it will be recompiled, which will recompile the template instance.
 
Share this answer
 
Comments
BernardIE5317 1-Oct-21 15:46pm    
Thank you for your reply I am surprised those who tout its speed benefits do not mention this restriction I can only conclude modules are of little use for faster builds as templates are common just as in the Standard Template Library So why does std.core exist? How is it an improvement over #includes<...>? Thank you again - Cheerio
Greg Utas 1-Oct-21 18:54pm    
Outside the STL, I think you'd find that templates aren't all that common.

The restriction that modules won't help template headers isn't mentioned because it's assumed that you know that a template header can't be pre-compiled. That's not a criticism, because it was a while before I realized this myself. I thought something magical was going on!

However, some compilers likely provide better performance by partially compiling templates. They would have to generate placeholders for template arguments and fill them in when instantiating each template instance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900