Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions


Reply
 
Thread Tools Search this Thread Display Modes
  #11  
Old 07-27-2015, 10:41 PM
Bill Rudolph Bill Rudolph is offline
Member
 
Join Date: Jan 2005
Posts: 42
Default

You asked about concepts. They allow the programmer to place constraints on types that can be used as template parameters for a given class or function. See https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)
Reply With Quote
  #12  
Old 07-27-2015, 11:06 PM
Duke of Chalfont Duke of Chalfont is offline
Member
SOA
 
Join Date: Apr 2011
Posts: 38
Default

Quote:
Originally Posted by actuary21c View Post
Hi Bill Rudolph

Have you used C#? If so, I'd be interested in your views on the following, given that you prefer C++.

I used C++ until C# came along. I prefer C# to C++ because:
- there is no need to have separate header and body files (.h and .cpp files in c++ always seemed to me like unnecessary duplication)
- it used to be (maybe this has improved) that you had to have separate files for each class in c++. So with say 50 classes (very common in my experience) that meant 100 files (again the doubling from .h and .cpp files), whereas you can put as many classes as you want into a c# .cs file
- c++ may be a tiny bit faster (typically 5% from what I've read) but that extra performance is not worth it compared to the advantages above which make writing code so much easier, and in any case, code optimisation is usually best done by finding the bottleneck and optimising that (in c++ if really needed)

in your reply to 2 above, please would you expand a bit on what you mean by "concepts"?

Thanks
I have used C#, considerably more than I would have liked to in fact, though still strongly prefer C++ (not that C++ is prefect or even close to it).

As is the case for C++, in C# the best practice still would be to have a single class (or at the most a collection of closely related classes) per .cs file. I honestly cannot see why anyone would want to do anything otherwise in all but the simplest of programs in any language. Java even requires having a single class per file. I will readily admit that header files in C++ are archaic and a little silly, but they are not that bad and do have some benefits, particularly in that they separate the "interface" from the implementation, which can potentially make C++ code easier to follow in larger projects.

As far as why C++ remains my favourite language, I enjoy its object oriented nature while still allowing for non-member (free) functions (leading to the potential of better encapsulation versus C#), the understated richness of the standard library, the ability to optimize memory use to the fullest extent possible (since you have complete control in choosing automatic allocation over dynamic allocation and due to the existence of RAII), and the power of templates. Unlike C#, (well-written) C++ will also compile and run without any pain on platforms other than Windows.

Last edited by Duke of Chalfont; 07-29-2015 at 07:56 AM..
Reply With Quote
  #13  
Old 07-28-2015, 11:28 PM
Bill Rudolph Bill Rudolph is offline
Member
 
Join Date: Jan 2005
Posts: 42
Default

As I mentioned earlier, you wrote "it used to be...that you had to have separate files for each class.... So with 50 classes...that meant 100 files (again the doubling from .h and .cpp files) ...."

I now see how you were coding (after looking at some books). This is old school stuff--C++ without templates. The programmer was encouraged to use a single header file or multiple header files. In your case, you used multiple headers--one with each source file.

I use the inclusion model for incorporating templates into the program. Care has to be taken in using this model. For example, I have the macros#ifndef HEADER_HPP, #define HEADER_HPP, and #endif as I do not want the header to be read more than once for a given translation unit (i.e., I don't want to break the one definition rule).
Reply With Quote
  #14  
Old 07-29-2015, 06:39 AM
Bill Rudolph Bill Rudolph is offline
Member
 
Join Date: Jan 2005
Posts: 42
Default

I just noticed that I can reduce the code a bit (766 lines now). I was not aware of std::is_same (new feature of C++11).

#ifndef MATRIX_HPP
#define MATRIX_HPP

#include <type_traits>

namespace bonkers {

template<class Q0,class Q1,class Q2>
class Matrix<numeric_tag,checked<Q0>,Q1,Q2>:public Matrix<simple_tag,checked<nope>,Q1,Q2> {

using Q_CHECKED=Q0;
using ELEMENT_TYPE=Q1;
using INPUT_TYPE=Q2;

using BASE=Matrix<simple_tag,checked<nope>,ELEMENT_TYPE, INPUT_TYPE>;

public:

const ELEMENT_TYPE& operator()(const INPUT_TYPE& t1,const INPUT_TYPE& t2) const
{
if(std::is_same<Q_CHECKED,yep>::value)
{
if(outside_constraints(t1,t2))
throw some_class();
}

return BASE::operator()(t1,t2);
}

};

}

#endif

Last edited by Bill Rudolph; 07-29-2015 at 09:05 AM..
Reply With Quote
Reply

Tags
.net, csharp, languages, programming, python

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT -4. The time now is 04:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.19780 seconds with 9 queries