Monday, 29 January 2018

c++ - Using Template Member data

I was trying to use a template based class as a member of another class. This other class will decide based on its data member value what is the data type that the template based member should use. To this end I used bit of polymorphism to decide on run time the instantiation.



class Base
{
public:
virtual void print() = 0;

};

template
class templateDynamic : public Base
{
public:
templateDynamic();
~templateDynamic();
void print();
};


template
templateDynamic::templateDynamic()
{
}

template
templateDynamic::~templateDynamic()
{
}


template
void templateDynamic::print()
{

}

class Holder
{
private:

Base * m_ABC;
public:
Holder(int a);
void print();
};

void Holder::print()
{
m_ABC->print();
}


Holder::Holder(int a)
{
if(a == 1)
m_ABC = new templateDynamic();
else
m_ABC = new templateDynamic();
}

int main()

{
Holder aHolder(1);
aHolder.print();
Holder aHolder2(2);
aHolder.print();
}


Print function will then print based on the type of T is it an int or float.
I am getting a linker error at this point.





error LNK2019: unresolved external symbol "public: __thiscall
templateDynamic::templateDynamic(void)"
(??0?$templateDynamic@H@@QAE@XZ) referenced in function "public:
__thiscall Holder::Holder(void)" (??0Holder@@QAE@XZ)


asp.net - Object reference not set to an instance - error on server after update/restart

I'm hoping you can help me. I'm getting the Object reference error on a webserver which contains an application we run.



Someone reset it this week and updated the server with (about) 50 new updates. I know. terrible. Anyway.. since then we're getting this error.




Server Error in '/' Application.



Object reference not set to an instance of an object.




Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.



Source Error:



Line 36: Hashtable items = mc.LanguageController.GetLanguageItems(ClientConfiguration.Language, languageItems);



Line 37: XmlElement itemsElem = doc.CreateElement("Lang");



Line 38: doc.DocumentElement.AppendChild(itemsElem);




Source File: c:\inetpub\wwwroot\xxxxxxxx\www\App_Code\Rendering\Rendering.cs Line: 36



Stack Trace:



[NullReferenceException: Object reference not set to an instance of an object.]



Rendering.GetRootDocument(String[] languageItems, IMainController mc) in c:\inetpub\wwwroot\xxxxxxxxxxxx\www\App_Code\Rendering\Rendering.cs:36



Rendering.GetDefaultDocument(IMainController mc) in c:\inetpub\wwwroot\xxxxxxxxxxxx\www\App_Code\Rendering\Rendering.cs:53




_Default.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\xxxxxxxxxxxx\www\Default.aspx.cs:31



System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14



System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35



System.Web.UI.Control.OnLoad(EventArgs e) +99



System.Web.UI.Control.LoadRecursive() +50




System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627



Version Information: Microsoft .NET Framework Version:2.0.50727.4216; ASP.NET Version:2.0.50727.4223




I've checked the Event log as well:




Event code: 3005




Event message: An unhandled exception has occurred.



Event time: 29-3-2012 12:47:38



Event time (UTC): 29-3-2012 10:47:38



Event ID: 69890cf8cf244b18a5adcbd29ae9a189



Event sequence: 25




Event occurrence: 8



Event detail code: 0



Application information:



Application domain: /LM/W3SVC/2/ROOT-3-129774906346518390



Trust level: Full




Application Virtual Path: /



Application Path: C:\inetpub\wwwroot\xxxxxxxx\www\



Machine name: WIN-PKNHIWTOENT



Process information:



Process ID: 4288




Process name: w3wp.exe



Account name: NT AUTHORITY\NETWORK SERVICE



Exception information:



Exception type: NullReferenceException



Exception message: Object reference not set to an instance of an object.




Request information:



Request URL: http://xxxxxxxx/default.aspx



Request path: /default.aspx



User host address: yyy.yyy.yyy.yyy



User:




Is authenticated: False



Authentication Type:



Thread account name: NT AUTHORITY\NETWORK SERVICE



Thread information:



Thread ID: 8




Thread account name: NT AUTHORITY\NETWORK SERVICE



Is impersonating: False



Stack trace: at Rendering.GetRootDocument(String[] languageItems, IMainController
mc) in c:\inetpub\wwwroot\xxxxxxxxxxxx\www\App_Code\Rendering\Rendering.cs:line 36



at Rendering.GetDefaultDocument(IMainController mc) in c:\inetpub\wwwroot\xxxxxxxxxxxx\www\App_Code\Rendering\Rendering.cs:line 53



at _Default.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\xxxxxxxxxxxx\www\Default.aspx.cs:line 31




at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t,
EventArgs e)



at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)



at System.Web.UI.Control.OnLoad(EventArgs e)



at System.Web.UI.Control.LoadRecursive()




at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint)




I suspect it's the languageItems variable, which is being passed from another function:




public static XmlDocument GetOverviewDocument(IMainController mc) {



   string[] languageItems = { 


"usermanagement",

"customermanagement",

"ordermanagement",

"ordermanagement LB",

"systemmanagement",


"qualitymanagement",

"overview",

"loggedinas",

"logout",

"Change password",


"managementoverview"

};

XmlDocument doc = GetRootDocument(languageItems, mc);

return doc;



}




Since I'm not an ASP guru like most of you I have no idea what's actually happening here; whether it's something to do with the updates to .NET which were installed, if it's code-syntax related or if there's a simple config mistake in web.config which I'm overlooking.



Any help would be most, MOST appreciated.

javascript - Detecting a mobile browser



I'm looking for a function which return boolean value if user has mobile browser or not.



I know that I can use navigator.userAgent and write that function by using regex, but user-agents are too various for different platforms. I doubt that match all possible devices would be easy, and I think this problem has been solved before many times so there should be some kind of complete solution for such task.



I was looking at this site, but sadly the script is so cryptic that I have no idea how to use it for my purpose, which is to create a function which return true / false.


Answer





Here's a function that uses an insanely long and comprehensive regex which returns a true or false value depending on whether or not the user is browsing with a mobile.



window.mobilecheck = function() {
var check = false;
(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};


For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:



window.mobileAndTabletcheck = function() {
var check = false;
(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};





The Original Answer



You can do this by simply running through a list of devices and checking if the useragent matches anything like so:



function detectmob() { 
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
return true;
}
else {
return false;
}
}


However since you believe that this method is unreliable, You could assume that any device that had a resolution of 800x600 or less was a mobile device too, narrowing your target even more (although these days many mobile devices have much greater resolutions than this)



i.e



function detectmob() {
if(window.innerWidth <= 800 && window.innerHeight <= 600) {
return true;
} else {
return false;
}
}


Reference:




Template instantiation in GNU C++ and Clang



Looks like the rules for template instantiation in Clang (3.8) and GNU C++ (4.9) are not the same. Here is an example:



#include 

template
class Assert {
Assert(); // private constructor for Assert

};

template <>
class Assert { // implicit public constructor for Assert
};

template
class A {
};


template
T foo(A) {
return T(N - 1);
}

template
T foo(A<0>) { // foo is not defined for N=0
Assert();
return T(0);
}


int main(int argc, char **argv) {
foo(A<3>());
return 0;
}


This minimal example shows a template function, foo, that is generalized over a type T and a natural number N. This function is not defined for N=0, so I'd like to use the Assert class to signal a compiler error if it is used this way.



This code is accepted by the GNU compiler (and by Visual C++ 2015, as well), but Clang gives an error for "calling a private constructor of class Assert".




So who is right? As I see it, there is no call for foo, so there is no need to instantiate this template...



EDIT: Accepting Clang's interpretation of the standard, what is a canonical way to enforce compile-time checks on template parameters?


Answer



I believe clang is correct, since Assert is not a dependent type.



http://en.cppreference.com/w/cpp/language/dependent_name





Non-dependent names are looked up and bound at the point of template definition. This binding holds even if at the point of template instantiation there is a better match:




Don't make specializations that cannot be valid. Make them general purpose and use static_assert (with a dependent value) to check for invalid template argument types/values. static_assert(std::is_same::value) or static_assert(N != 0)


list - Negative indexing in Python




I have one record in a list



>>> bob =['bob smith',42,30000,'software']


I am trying to get the last name 'smith' from this record



I use the below command:




>>> bob[0].split()[1]


It provides me 'smith'



But the book I am referring to use the below command:



>>> bob[0].split()[-1]



it also gives me same result 'smith'



Why do indexes [1] and [-1] provide the same result?


Answer



Python lists can be "back indexed" using negative indices. -1 signifies the last element, -2 signifies the second to last and so on. It just so happens that in your list of length 2, the last element is also the element at index 1.



Your book suggests using -1 because it is more appropriate from a logical standpoint. You don't want the item at index 1 per se, but rather the last element of the list, which is the last name. What if, for example, a middle name was also included? Then using an index of 1 would not work whereas an index of -1 would.


apache redirect from non www to www



I have a website that doesn't seem to redirect from non-www to www.




My Apache configuration is as follows:



RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]


What am I missing?



Answer



Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:




ServerName example.com
Redirect permanent / http://www.example.com/



ServerName www.example.com

# real server configuration



And then you'll have another section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).


How to avoid using Select in Excel VBA

Some examples of how to avoid select


Use Dim'd variables


Dim rng as Range

Set the variable to the required range. There are many ways to refer to a single-cell range


Set rng = Range("A1")
Set rng = Cells(1,1)
Set rng = Range("NamedRange")

or a multi-cell range


Set rng = Range("A1:B10")
Set rng = Range("A1", "B10")
Set rng = Range(Cells(1,1), Cells(10,2))
Set rng = Range("AnotherNamedRange")
Set rng = Range("A1").Resize(10,2)

You can use the shortcut to the Evaluate method, but this is less efficient and should generally be avoided in production code.


Set rng = [A1]
Set rng = [A1:B10]

All the above examples refer to cells on the active sheet. Unless you specifically want to work only with the active sheet, it is better to Dim a Worksheet variable too


Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Cells(1,1)
With ws
Set rng = .Range(.Cells(1,1), .Cells(2,10))
End With

If you do want to work with the ActiveSheet, for clarity it's best to be explicit. But take care, as some Worksheet methods change the active sheet.


Set rng = ActiveSheet.Range("A1")

Again, this refers to the active workbook. Unless you specifically want to work only with the ActiveWorkbook or ThisWorkbook, it is better to Dim a Workbook variable too.


Dim wb As Workbook
Set wb = Application.Workbooks("Book1")
Set rng = wb.Worksheets("Sheet1").Range("A1")

If you do want to work with the ActiveWorkbook, for clarity it's best to be explicit. But take care, as many WorkBook methods change the active book.


Set rng = ActiveWorkbook.Worksheets("Sheet1").Range("A1")

You can also use the ThisWorkbook object to refer to the book containing the running code.


Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1")

A common (bad) piece of code is to open a book, get some data then close again


This is bad:


Sub foo()
Dim v as Variant
Workbooks("Book1.xlsx").Sheets(1).Range("A1").Clear
Workbooks.Open("C:\Path\To\SomeClosedBook.xlsx")
v = ActiveWorkbook.Sheets(1).Range("A1").Value
Workbooks("SomeAlreadyOpenBook.xlsx").Activate
ActiveWorkbook.Sheets("SomeSheet").Range("A1").Value = v
Workbooks(2).Activate
ActiveWorkbook.Close()
End Sub

And would be better like:


Sub foo()
Dim v as Variant
Dim wb1 as Workbook
Dim wb2 as Workbook
Set wb1 = Workbooks("SomeAlreadyOpenBook.xlsx")
Set wb2 = Workbooks.Open("C:\Path\To\SomeClosedBook.xlsx")
v = wb2.Sheets("SomeSheet").Range("A1").Value
wb1.Sheets("SomeOtherSheet").Range("A1").Value = v
wb2.Close()
End Sub

Pass ranges to your Subs and Functions as Range variables


Sub ClearRange(r as Range)
r.ClearContents
'....
End Sub
Sub MyMacro()
Dim rng as Range
Set rng = ThisWorkbook.Worksheets("SomeSheet").Range("A1:B10")
ClearRange rng
End Sub

You should also apply Methods (such as Find and Copy) to variables


Dim rng1 As Range
Dim rng2 As Range
Set rng1 = ThisWorkbook.Worksheets("SomeSheet").Range("A1:A10")
Set rng2 = ThisWorkbook.Worksheets("SomeSheet").Range("B1:B10")
rng1.Copy rng2

If you are looping over a range of cells it is often better (faster) to copy the range values to a variant array first and loop over that


Dim dat As Variant
Dim rng As Range
Dim i As Long
Set rng = ThisWorkbook.Worksheets("SomeSheet").Range("A1:A10000")
dat = rng.Value ' dat is now array (1 to 10000, 1 to 1)
for i = LBound(dat, 1) to UBound(dat, 1)
dat(i,1) = dat(i,1) * 10 'or whatever operation you need to perform
next
rng.Value = dat ' put new values back on sheet

This is a small taster for what's possible.

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...