How to "pass" an namespace as argument? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If I have the following code :
void foo1()
{
NS1::Type1 instance1;
NS1::Type2 instance2;
NS1::Type3 instance3;
}
void foo2()
{
NS2::Type1 instance1;
NS2::Type2 instance2;
NS2::Type3 instance3;
}
How to factorize this function ?
I can call foo1 from NS1, and foo2 from NS2.

How to “pass” an namespace as argument?
There is no way to do that.
It is possible to write a reusable template for your foos if you use classes instead of namespaces:
struct NS1 {
using Type1 = int;
using Type2 = float;
using Type3 = std::string;
};
struct NS2 {
using Type1 = long;
using Type2 = double;
using Type3 = std::string;
};
template<class T>
void foo() {
typename T::Type1 instance1;
typename T::Type2 instance2;
typename T::Type3 instance3;
}

Related

How to move functions into a namespace? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
The project calls functions from a library. I want to move those functions behind a namespace so that is easier to spot the places on the codebase where those functions are being called.
How functions are being called:
#include "foo.h"
int main()
{
foo();
bar();
return 0;
}
How I want to call them:
#include "myfoo.h"
int main()
{
thatlibrary::my_foo();
thatlibrary::my_bar();
return 0;
}
How I implemented that:
myfoo.h
namespace thatlibrary
{
void my_foo();
void my_bar();
}
myfoo.cpp
namespace thatlibrary
{
void my_foo()
{
foo();
}
void my_bar()
{
bar();
}
}
Wondering if there is any other solution? Perhaps more elegant.
Just use a using declaration in your namespace:
// global namespace
int foo() {
return 1;
}
namespace thatlibrary {
using ::foo;
}
auto i = thatlibrary::foo();
Notice that the name foo is still available in the global namespace, though.
// in global namespace
auto j = foo(); // works just fine
// from anywhere
auto k = ::foo(); // works just fine
I think the library is poorly designed, if it declares all its stuff in the global namespace.

How to work with a vector array of struct? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I have this struct:
struct foo{
DWORD fooB;
char *fooA;
}
and I have a variable DWORD bar;, so how do I find if bar matches any fooB in my struct?
EDIT: my Code (currently)
#include <algorithm> // for. std::find
using namesapce std;
struct foo{
DWORD fooB;
char *fooA;
// .... Use this
}
vector <DWORD> foo;
if ( std::find(vector.begin(),
vector.end(), pIdToFind) !=
vector.end() )
// We found the item in the list, so let's just continue
else
// We haven't found it,
You could simply provide a comparison operator for comparing DWORDs to foos:
#include <vector>
#include <algorithm>
#include <windows.h>
struct foo {
DWORD fooB;
char *fooA;
};
bool operator==(DWORD lhs, foo const &rhs)
{
return lhs == rhs.fooB;
}
int main()
{
foo needle{ 42, nullptr };
vector<DWORD> haystack;
if (std::find(haystack.begin(), haystack.end(), needle) != haystack.end())
{
// We found the item in the list, so let's just continue
}
else
{
// not found
}
}

Create a variable inside another statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}

declare function's type as struct c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have this class named User. Inside I have made a struct called flights and I want to make a function in this class to return a struct with the values of flights. Is that possible? Something like the follow. I know it doesn't work but is there a way?
Class User
{
string name, surname...;
struct flights
{
int miles;
double cost;
}
struct add_miles(reads from another class);
}
struct User::add_miles()
{
return flights;
}
I am not sure I understand your requirement clearly but see my try:
#include<iostream>
class User
{
//string name, surname...;
public:
struct flights
{
int miles;
double cost;
}myflights;
struct flights add_miles()
{
return myflights;
}
};
int main()
{
User me;
me.myflights.miles=100;
std::cout<<me.add_miles().miles;
}
You're going to have to make add_miles return a flights struct rather than just a struct.
Example code:
#include <iostream>
using namespace std;
class User
{
private:
string name,
surname;
public:
struct flights {
int miles;
double cost;
};
flights add_miles() {
return flights();
}
};
int main(int argc, char **argv) {
User u;
User::flights f = u.add_miles();
cout << f.miles << endl;
return 0;
}

recursive_wrapper and ambiguous convert_construct() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Do you have any idea how to solve or get around this problem?
http://boost.2283326.n4.nabble.com/Variant-recursive-wrapper-and-ambiguous-convert-construct-td4543139.html
#include <boost/variant.hpp>
class b_fwd;
typedef boost::variant<boost::recursive_wrapper<b_fwd> > a;
class c_fwd;
typedef boost::variant<boost::recursive_wrapper<c_fwd> > b;
struct b_fwd : b {
// inherit constructors
template <typename... Ts> b_fwd(Ts... o) : b(o...) {}
};
typedef boost::variant<double, char *> c;
struct c_fwd : c {
// inherit constructors
template <typename... Ts> c_fwd(Ts... o) : c(o...) {}
};
void f(const b &b_node)
{
a a_node(b_node);
}
int main()
{
}
I had a hunch in reaction to GuyGreer's comment.
If the problem is caused by variant being assignable/convertible to variant, then maybe we can disambiguate by using a third, isolated, variant:
a a_node(boost::variant<b>(b_node));
Lo and behold, homeopathy seems to work :)
See it Live On Coliru
I have no idea whether this "works" in the sense that the OP expects, because, frankly, the OP's code was a mystery to me
#include <boost/variant.hpp>
class b_fwd;
typedef boost::variant<boost::recursive_wrapper<b_fwd> > a;
class c_fwd;
typedef boost::variant<boost::recursive_wrapper<c_fwd> > b;
struct b_fwd : b {
using b::b;
};
typedef boost::variant<double, char *> c;
struct c_fwd : c {
using c::c;
};
void f(const b &b_node)
{
a a_node(boost::variant<b>(b_node));
}
int main()
{
}