This question already has answers here:
Is there a SASS.js? Something like LESS.js?
(7 answers)
Apply a class AND an attribute selector
(2 answers)
Closed 7 years ago.
I'm trying to get something similar to this SASS code to work;
.alert-danger {
#include alert-variant($alert-danger-bg, $alert-danger-border, $alert-danger-text);
#include transition(ease-in-out, 95ms, all);
#if data-interaction == true {
&:hover,
&:focus {
#include alert-variant-interactive($alert-danger-bg, $alert-danger-border, $alert-danger-text);
}
}
}
I can't find a good way to check "if" some attribute or class exists within .alert-danger, though. I would really like to keep the hover nested, instead of having to write extra blocks.
Right now I am using data-interaction="true" on the .alert-danger element.
Thanks in advance!
Solution I found
Happily I did find my own solution so I am posting it here in case others are having the same issue!
The code should have looked like this, instead of what was written above:
.alert-danger {
#include alert-variant($alert-danger-bg, $alert-danger-border, $alert-danger-text);
#include transition(ease-in-out, 95ms, all);
&[data-interaction="true"] {
&:hover,
&:focus {
#include alert-variant-interactive($alert-danger-bg, $alert-danger-border, $alert-danger-text);
}
}
}
I changed the #if directive I used &[data-attribute="true"], which appends the attribute required for the nested values to compile. :)
Again, to the statements above; yes, it is a pre-processor but of course that means if CSS can do something, SASS can surely do it. All a matter of finding the right method. :)
Related
One very experienced programmer wrote something like this:
#include <string>
using namespace std;
class Fraction
{
float nominator;
float denominator;
void load()
{
cin>>nominator; cin>>denominator;
if(denominator==0)
{
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
}
}
};
I have no idea why there is an if statement. Is it really necessary?
In this particular example,
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
would be exactly equivalent.
In the context you provided, nothing can tell us why someone would nest that loop in an useless if, but one could come up with explanations. In earlier version of that code, something could have been present inside the if block that changed the behavior of the program/function.
It also could be an innocent error.
Stroustrup's cat might have walked on their keyboard.
Is it really necessary?
No, it is not necessary.
The shown snippet is equivalent to a program where the if statement is replaced with the contained loop statement.
From this code:
if(denominator==0)
{
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
}
I think if is only used for a teaching purpose to simply show that how flow will go to while after checking if condition. And how while loop will work further.
But if is not really required here.
This code do the same:
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
This question already has answers here:
Start thread with member function
(5 answers)
Closed 8 years ago.
I'm trying to make a class with a method that calls other method (that is in the same class) multiple times using multithreading. The code is something like this:
#include <iostream>
#include <thread>
using namespace std;
class ThreadedMatcher
{
float Match()
{
thread t[5];
//5 is just as an aleatory number
//The error doesn't change if I use a pointer (like thread *t;)
for (int i = 0; i < num_jobs; i++)
{
t[i](partialMatch,i);
}
}
void partialMatch(int i){
//Whathever I put in here doesn't change the error
}
}
(this code is written in "ThreadedMatcher.h")
When I compile this, the next two errors appear:
error c3867: 'ThreadedMatcher::partialMatch': function call missing argument list; use '&ThreadedMatcher::partialMatch' to create a pointer to member
error c2064: term does not evaluate to a function taking 2 arguments
(these two errors refers to the part inside the for bucle)
If I follow the advise in the first error, the second error stays there anyway.
Could anyone tell me how to solve this? I'm using visual studio 2012 (c++11), in windows 8.
Thank you for any help you can provide.
PS: Sorry for my bad english, I did the best I could
To reference a function in a class, you must reference the class in which the function resides.
change your thread execution to pass by reference.
This SO question
Explains it nicely
From that question
void Test::runMultiThread()
{
std::thread t1(&Test::calculate, this, 0, 10);
std::thread t2(&Test::calculate, this, 11, 20);
t1.join();
t2.join();
}
This question already has answers here:
Nested Comments in C++
(4 answers)
Closed 9 years ago.
While this problem can apply to other languages I am looking for solutions that can apply to C++ language.
The problem is, when we comment a block like:
void doStuff() {
cout<<"doing stuff";
/*start();
cout<<"done";*/
}
The comment out works completely fine, however what I am wondering is when I want to comment out entire function by:
/*void doStuff() {
cout<<"doing stuff";
/*start();
cout<<"done";*/
}*/
It obviously doesn't work. The only way that I know to make it work is either:
/*void doStuff() {
cout<<"doing stuff";
start();
cout<<"done";
}*/
OR
/*void doStuff() {
cout<<"doing stuff";
*//*start();
cout<<"done";*//*
}*/
I know that there are IDE's that can automatically insert // at each line and then get rid off it but that's not what I am after.
My question is, is there any easier way of escaping from inner commented out blocks?
You can't do it with comment grammer.
Use #if 0 #endif to comment out a large portion of code with /* */ inside.
I'm trying to parse an HTML file for a C++ assignment. The assignment is demonstrating stacks; we're supposed to push to the stack every time we hit a tag, and then pop off when we find the corresponding closing tag.
The teacher obviously wants us to hard-code a set of tags to detect, like:
// Declare some stacks
Stack html;
Stack div;
...
// When you find an open tag, push to the corresponding stack
if (tagcontents == "html") { html.push(); }
if (tagcontents == "div") { div.push(); }
...
// When you find a close tag, push to the corresponding stack
if (tagcontents == "/html") { html.pop(); }
if (tagcontents == "/div") { div.pop(); }
...
The obvious downside of this is that if I want to support all of the tags available in HTML, I can expect to do lots of redundant coding. The teacher obviously wants us to pick just a small subset of the available tags, and go off those, but I think that's lame. Since I'm lazy (and I firmly believe that all programmers should be), I'm trying to come up with a dynamic solution.
The idea is, whenever I encounter a new tag, create a stack for it. This would allow my program to support ANY tag, regardless of validity. I'm hitting an interesting theoretical problem, though, and I'm not even sure what to call it in order to research it. Namely, that I need to use the VALUE of a variable as part of my actual code. IE:
if (no stack exists named "HTML") { create a stack named "HTML" }
In simplistic terms, how can I:
tag = "html";
Stack tag; // make a stack named HTML?
Or is there another way to do this? Any help would be greatly appreciated. If I can't figure this out, I'll probably just use a switch/case statement like a quitter.
Create the stacks inside a std::map<std::string, Stack>.
use a map/unordered map:
std::map <std:string, Stack> myStacks;
Then you can just do
myMastacks[tagcontents].push()
This will initialize a new stack for the key if one does not yet exist.
and on the end of tag, strip the slash, check if it's on the map, and there you go.
I would do it differently, more simple, with only one stack for all tags (which I think is very reasonable, unless your teacher actually instructed you to use several stacks): Declare a stack of strings. A string represents a tag. You can use the STL stack for this:
stack<string> my_tags;
my_tags.push("div") will push "div" into the stack.
string tag = my_tags.top(); will query the top of the stack, and my_tags.pop() will pop the top item from the stack. Very easy :-)
Again, this solution is good in case you don't really need to practice several stacks, but to examine where you stand within the html parsing.
Here is an example:
#include <stdio.h>
#include <map>
#include <string>
#include <list>
#include <iostream>
typedef std::list<std::string> stack;
typedef std::map<std::string, stack> stack_map;
stack_map my_stacks;
stack& getStack(const std::string& stack_name) {
stack_map::iterator it = my_stacks.find(stack_name);
if( it != my_stacks.end() ) {
return it->second;
} else {
my_stacks[stack_name] = stack();
return my_stacks[stack_name];
}
}
...
stack& div_stack = getStack("div");
// and use that for example
div_stack.push_back("some info");
div_stack.push_back("some more info ... ");
div_stack.push_back("s even more ... ");
.....
I am trying to create a macro that takes a scope as a parameter.
I know, it is probably not a good thing etc etc.
I was trying this and got the problem that preprocessor looks for commas and parentheses... the problem is with enum.
How would I declare a enum inside a scope that is a parameter of a macro?
when the compiler see the comma between enum itens, it takes it as a separator.
If you are curious to know why I entered into this, is because I need to register my namespaces and classes, for namespaces I need to know when they are closed, so I was thinking to create a macro that initially calls a static function that register the namespace, encapsulate its contents and finally call a static function that removes the namespace from the registry.
With a macro it would be easier for the coder to do this and make sure he doesn't forget to remove the namespace in the end of the bracket.
Thanks,
Joe
EDIT:
I want a macro that accepts a scope as parameters:
#define MYMACRO(unkownscope) unknownscope
class MYMACRO({
// please, don't take this code seriously, it is just an example so you can understand my question
});
now, if I try:
#define MYMACRO(unkownscope) unknownscope
class MYMACRO({
enum {
anything = 1,
everything = 2
};
});
it won't compile because of the comma inside the enum, because the compiler thinks it is a separator of the macro. It doesn't happen with commas inside parentheses, example:
int a(){
int x = anyfunction(1, 2);
}
would compile normally because the comma is inside a double parentheses.
Sorry for not being able to explain earlier... my english is not that good and the words just keep skipping me =[
Ty for the answers!
Joe
It sounds like you are pushing the preprocessor beyond where it's willing to go. While it's not as elegant, how about breaking your macro in two (one pre- and one post-) and rather then passing a "scope" as parameter, you surround your scope with you pre- and post- macros.
So, if your macro looks something like:
SOMACRO({ ... });
You would instead do something like:
PRESOMACRO();
{ ... };
POSTSOMACRO();
#define SCOPED_STUFF(pre,post) pre; STUFF; post;
#define STUFF enum {a,b,c}
SCOPED_STUFF(a,b)
#undef STUFF
#define STUFF enum {q,r}
SCOPED_STUFF(c,d)
#undef STUFF
You are attempting to replicate RAII with a macro.
#define SCOPE(ns) NamespaceRegistrar _ns_rar(ns);
struct NamespaceRegistrar {
std::string _ns;
NamespaceRegistrar(const std::string& ns) : _ns(ns) { AcquireResource(_ns); }
~NamespaceRegistrar() { ReleaseResource(_ns); }
};
{
SCOPE("Foo")
// stuff
}
I have no idea what you are talking about with regard to enums.
You already noticed what the problem is, an article on boostpro.com sums the problem up.
There are work-arounds, but i'd go for utilizing Boost.Preprocessor.
Without knowing exactly what you're trying to achieve syntactically, something like this might be what you are looking for (edited to PP_SEQ):
#define MAKE_ENUM(Name, Seq) enum Name { BOOST_PP_SEQ_ENUM(Seq) }
MAKE_ENUM(foo, (a)(b)(c));