Template code - and is this redundancy or intended? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)IniReader template overrides become ambiguousCould this template function be improved?Stopwatch templateArray-like container for uints shorter than 8 bits (Rev 1)Making my underscore template code more manageableSpecialized template methods with complex numbers classSimple multi-dimensional Array class in C++11 - follow-upA template-fixed-size unsigned integer classC++ std::array wrapperC++ wrapper class to mimic a C array's brace initialization
Change bounding box of math glyphs in LuaTeX
Does Parliament hold absolute power in the UK?
Do working physicists consider Newtonian mechanics to be "falsified"?
ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?
Does the AirPods case need to be around while listening via an iOS Device?
First use of “packing” as in carrying a gun
Would an alien lifeform be able to achieve space travel if lacking in vision?
"... to apply for a visa" or "... and applied for a visa"?
Can withdrawing asylum be illegal?
Clean Corners for Subdivision
How to read αἱμύλιος or when to aspirate
How does ice melt when immersed in water
Did the new image of black hole confirm the general theory of relativity?
Is there a way to generate uniformly distributed points on a sphere from a fixed amount of random real numbers per point?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Is every episode of "Where are my Pants?" identical?
How to support a colleague who finds meetings extremely tiring?
How to delete random line from file using Unix command?
Why can't wing-mounted spoilers be used to steepen approaches?
system call string length limit
Working through the single responsibility principle (SRP) in Python when calls are expensive
best way to run sql server management studio on linux
Windows 10: How to Lock (not sleep) laptop on lid close?
Sort list of array linked objects by keys and values
Template code - and is this redundancy or intended?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)IniReader template overrides become ambiguousCould this template function be improved?Stopwatch templateArray-like container for uints shorter than 8 bits (Rev 1)Making my underscore template code more manageableSpecialized template methods with complex numbers classSimple multi-dimensional Array class in C++11 - follow-upA template-fixed-size unsigned integer classC++ std::array wrapperC++ wrapper class to mimic a C array's brace initialization
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I would like to discuss this code which is used in a PDE solver framework I found online. I don't quiet understand why I would need to introduce this form of code duplication.
template <std::size_t N, typename T>
class MathVector
public:
typedef T value_type;
typedef std::size_t size_type;
static const std::size_t Size = N;
public:
MathVector()
MathVector(const value_type& val) for(std::size_t i = 0; i < N; ++i) m_data[i] = val;
// negation
MathVector& operator- () for(std::size_t i = 0; i < N; ++i) m_data[i] = -m_data[i]; return *this;
Then there is classes for dimension 1 and 2...
template <typename T>
class MathVector<1, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 1;
public:
MathVector()
MathVector(value_type x)
m_data[0] = x;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; return *this;
template <typename T>
class MathVector<2, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 2;
public:
MathVector()
MathVector(value_type x, value_type y)
m_data[0] = x;
m_data[1] = y;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; m_data[1] = -m_data[1]; return *this;
and for 3 and 4 analogue to that. I'm wondering why I would duplicate this. Wouldn't the first template class be sufficient?
c++ template
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I would like to discuss this code which is used in a PDE solver framework I found online. I don't quiet understand why I would need to introduce this form of code duplication.
template <std::size_t N, typename T>
class MathVector
public:
typedef T value_type;
typedef std::size_t size_type;
static const std::size_t Size = N;
public:
MathVector()
MathVector(const value_type& val) for(std::size_t i = 0; i < N; ++i) m_data[i] = val;
// negation
MathVector& operator- () for(std::size_t i = 0; i < N; ++i) m_data[i] = -m_data[i]; return *this;
Then there is classes for dimension 1 and 2...
template <typename T>
class MathVector<1, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 1;
public:
MathVector()
MathVector(value_type x)
m_data[0] = x;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; return *this;
template <typename T>
class MathVector<2, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 2;
public:
MathVector()
MathVector(value_type x, value_type y)
m_data[0] = x;
m_data[1] = y;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; m_data[1] = -m_data[1]; return *this;
and for 3 and 4 analogue to that. I'm wondering why I would duplicate this. Wouldn't the first template class be sufficient?
c++ template
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Sufficient for what?
$endgroup$
– Juho
12 mins ago
$begingroup$
Sorry, the missing code was not saved.
$endgroup$
– stephanmg
10 mins ago
add a comment |
$begingroup$
I would like to discuss this code which is used in a PDE solver framework I found online. I don't quiet understand why I would need to introduce this form of code duplication.
template <std::size_t N, typename T>
class MathVector
public:
typedef T value_type;
typedef std::size_t size_type;
static const std::size_t Size = N;
public:
MathVector()
MathVector(const value_type& val) for(std::size_t i = 0; i < N; ++i) m_data[i] = val;
// negation
MathVector& operator- () for(std::size_t i = 0; i < N; ++i) m_data[i] = -m_data[i]; return *this;
Then there is classes for dimension 1 and 2...
template <typename T>
class MathVector<1, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 1;
public:
MathVector()
MathVector(value_type x)
m_data[0] = x;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; return *this;
template <typename T>
class MathVector<2, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 2;
public:
MathVector()
MathVector(value_type x, value_type y)
m_data[0] = x;
m_data[1] = y;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; m_data[1] = -m_data[1]; return *this;
and for 3 and 4 analogue to that. I'm wondering why I would duplicate this. Wouldn't the first template class be sufficient?
c++ template
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I would like to discuss this code which is used in a PDE solver framework I found online. I don't quiet understand why I would need to introduce this form of code duplication.
template <std::size_t N, typename T>
class MathVector
public:
typedef T value_type;
typedef std::size_t size_type;
static const std::size_t Size = N;
public:
MathVector()
MathVector(const value_type& val) for(std::size_t i = 0; i < N; ++i) m_data[i] = val;
// negation
MathVector& operator- () for(std::size_t i = 0; i < N; ++i) m_data[i] = -m_data[i]; return *this;
Then there is classes for dimension 1 and 2...
template <typename T>
class MathVector<1, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 1;
public:
MathVector()
MathVector(value_type x)
m_data[0] = x;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; return *this;
template <typename T>
class MathVector<2, T>
public:
typedef std::size_t size_type;
typedef T value_type;
static const std::size_t Size = 2;
public:
MathVector()
MathVector(value_type x, value_type y)
m_data[0] = x;
m_data[1] = y;
// negation
MathVector& operator- () m_data[0] = -m_data[0]; m_data[1] = -m_data[1]; return *this;
and for 3 and 4 analogue to that. I'm wondering why I would duplicate this. Wouldn't the first template class be sufficient?
c++ template
c++ template
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 5 mins ago
stephanmg
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 13 mins ago
stephanmgstephanmg
12
12
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
stephanmg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
Sufficient for what?
$endgroup$
– Juho
12 mins ago
$begingroup$
Sorry, the missing code was not saved.
$endgroup$
– stephanmg
10 mins ago
add a comment |
$begingroup$
Sufficient for what?
$endgroup$
– Juho
12 mins ago
$begingroup$
Sorry, the missing code was not saved.
$endgroup$
– stephanmg
10 mins ago
$begingroup$
Sufficient for what?
$endgroup$
– Juho
12 mins ago
$begingroup$
Sufficient for what?
$endgroup$
– Juho
12 mins ago
$begingroup$
Sorry, the missing code was not saved.
$endgroup$
– stephanmg
10 mins ago
$begingroup$
Sorry, the missing code was not saved.
$endgroup$
– stephanmg
10 mins ago
add a comment |
0
active
oldest
votes
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: "196"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
stephanmg is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2fcodereview.stackexchange.com%2fquestions%2f217403%2ftemplate-code-and-is-this-redundancy-or-intended%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
stephanmg is a new contributor. Be nice, and check out our Code of Conduct.
stephanmg is a new contributor. Be nice, and check out our Code of Conduct.
stephanmg is a new contributor. Be nice, and check out our Code of Conduct.
stephanmg is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2fcodereview.stackexchange.com%2fquestions%2f217403%2ftemplate-code-and-is-this-redundancy-or-intended%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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
$begingroup$
Sufficient for what?
$endgroup$
– Juho
12 mins ago
$begingroup$
Sorry, the missing code was not saved.
$endgroup$
– stephanmg
10 mins ago